技术标签: 登录注册 Android 学习笔记 SQLite Android Studio
声明:AS版本为:3.4;JDK版本为:1.8
1、先看一下项目目录:
2、新建一个AS项目,创建如上图所示的目录结构,然后添加内容:
(1)修改添加布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="欢迎进入登录界面!"
android:textSize="30dp"
android:textStyle="bold" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="用户名:" />
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名!!!" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="密码:" />
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入密码!!!" />
</TableRow>
<TableRow>
<TextView />
<LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="登录" />
<Button
android:id="@+id/register"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RegisterActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="欢迎进入注册界面!"
android:textSize="30dp"
android:textStyle="bold" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="用户名:" />
<EditText
android:id="@+id/usernameRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名!!!" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="密码:" />
<EditText
android:id="@+id/passwordRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入密码!!!" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="年龄:" />
<EditText
android:id="@+id/ageRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入年龄!!!" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="性别:"
android:textSize="20dp" />
<RadioGroup
android:id="@+id/sexRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/woman"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/nan"
android:text="男"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<RadioButton
android:id="@id/woman"
android:text="女"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RadioGroup>
</TableRow>
<TableRow >
<TextView />
<LinearLayout >
<Button
android:id="@+id/Register"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
(2)在service包DatabaseHelper中添加链接AS自带数据库以及创建表的语句:
package com.example.sqlitelogin.service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
static String name="user.db";
static int dbVersion=1;
public DatabaseHelper(Context context) {
super(context, name, null, dbVersion);
}
public void onCreate(SQLiteDatabase db) {
String sql="create table user(id integer primary key autoincrement,username varchar(20),password varchar(20),age integer,sex varchar(2))";
db.execSQL(sql);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
(3)在service包UserService中用sql语句写登录注册功能的实现:
package com.example.sqlitelogin.service;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.sqlitelogin.User;
public class UserService {
private DatabaseHelper dbHelper;
public UserService(Context context){
dbHelper=new DatabaseHelper(context);
}
public boolean login(String username,String password){
SQLiteDatabase sdb=dbHelper.getReadableDatabase();
String sql="select * from user where username=? and password=?";
Cursor cursor=sdb.rawQuery(sql, new String[]{username,password});
if(cursor.moveToFirst()==true){
cursor.close();
return true;
}
return false;
}
public boolean register(User user){
SQLiteDatabase sdb=dbHelper.getReadableDatabase();
String sql="insert into user(username,password,age,sex) values(?,?,?,?)";
Object obj[]={user.getUsername(),user.getPassword(),user.getAge(),user.getSex()};
sdb.execSQL(sql, obj);
return true;
}
}
(4)在User文件中声明要用到的表列名的变量,并对其添加get&&set方法:
package com.example.sqlitelogin;
import java.io.Serializable;
public class User implements Serializable{
private int id;
private String username;
private String password;
private int age;
private String sex;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String username, String password, int age, String sex) {
super();
this.username = username;
this.password = password;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", age=" + age + ", sex=" + sex + "]";
}
}
(5)为注册功能添加activity组件:
package com.example.sqlitelogin;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.sqlitelogin.service.UserService;
public class RegisterActivity extends AppCompatActivity {
EditText username;
EditText password;
EditText age;
RadioGroup sex;
Button register;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
findViews();
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name=username.getText().toString().trim();
String pass=password.getText().toString().trim();
String agestr=age.getText().toString().trim();
String sexstr=((RadioButton)RegisterActivity.this.findViewById(sex.getCheckedRadioButtonId())).getText().toString();
Log.i("TAG",name+"_"+pass+"_"+agestr+"_"+sexstr);
UserService uService=new UserService(RegisterActivity.this);
User user=new User();
user.setUsername(name);
user.setPassword(pass);
user.setAge(Integer.parseInt(agestr));
user.setSex(sexstr);
uService.register(user);
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
}
});
}
private void findViews() {
username=(EditText) findViewById(R.id.usernameRegister);
password=(EditText) findViewById(R.id.passwordRegister);
age=(EditText) findViewById(R.id.ageRegister);
sex=(RadioGroup) findViewById(R.id.sexRegister);
register=(Button) findViewById(R.id.Register);
}
}
(6)为登录功能添加activity组件:
package com.example.sqlitelogin;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.sqlitelogin.service.UserService;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//即activity_login.xml
findViews();
}
private EditText username;
private EditText password;
private Button login;
private Button register;
private void findViews() {
username=(EditText) findViewById(R.id.username);
password=(EditText) findViewById(R.id.password);
login=(Button) findViewById(R.id.login);
register=(Button) findViewById(R.id.register);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=username.getText().toString();
System.out.println(name);
String pass=password.getText().toString();
System.out.println(pass);
Log.i("TAG",name+"_"+pass);
UserService uService=new UserService(LoginActivity.this);
boolean flag=uService.login(name, pass);
if(flag){
Log.i("TAG","登录成功");
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(intent);
}else{
Log.i("TAG","登录失败");
Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
}
}
});
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(intent);
}
});
}
}
3、Androidmanifest.xml清单文件中,程序运行必备的内容一般都已经自动完成添加了。也可以进行修改:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sqlitelogin">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".RegisterActivity">
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
</activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--<uses-library android:name="android.test.runner"/>-->
</application>
</manifest>
4、在模拟器或者真机运行程序,即可!一个连接数据库的登录注册功能已经实现,效果如下:
如果登录、注册的两个布局文件的 Preview 视图标红,将 android.support.constraint.ConstraintLayout 替换为 LinearLayout 即可
喜 欢 请 点 赞 哟 |
(●ˇ∀ˇ●) |
文章浏览阅读7.4w次,点赞327次,收藏2.3k次。玩转传感器——DHT11温湿度传感器(STM32版)文章目录玩转传感器——DHT11温湿度传感器(STM32版)前言一、接口说明1 接线图2 电源引脚3 串行接口(单线双向)二、通信过程三、测量分辨率与电气特性四、使用注意事项1 工作与贮存条件2 暴露在化学物质中3 恢复处理4 温度影响5 光线6 配线注意事项五、DHT11驱动程序1 DHT11.c1.1 配置输入输出GPIO1.2 复位DHT111.3 检查DHT11是否正常1.4 DHT11初始化1.5 读取一位数据(返回值0/1)1.6 读取一个_温湿度传感器接线图
文章浏览阅读905次。本说明以NVP6324为例。1、首先在drivers\media\i2c中修改Kconfig和Makefile,如下: 在Kconfig中添加如下:config VIDEO_NVP6324 tristate "NVP6324 AHD sensor support" depends on I2C ---help--- This is a V4L2 sensor-le..._nvp6324 驱动
文章浏览阅读2.7w次,点赞9次,收藏80次。理论请参考博客OpenCV源码的本地路径: %OPENCV%\opencv\sources\modules\imgproc\src\clahe.cppclahe.cpp// ----------------------------------------------------------------------// CLAHEnamespace{ class C_自适应双平台直方图均衡算法代码
文章浏览阅读3.1k次,点赞3次,收藏22次。点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达作者丨吃猫的鱼python @CSDN编辑丨3D视觉开发者社区目录content一、什么是计算机视觉二、图片处理基础操作图片处理:读入图像图片处理:显示图像图片处理:图像保存三、图像处理入门基础图像成像原理介绍图像分类四、像素处理操作读取像素修改像素使用python中的numpy修改像素点五、获取图像属性形状像素数目图像类型六..._计算机视觉与图像处理
文章浏览阅读7.4k次,点赞6次,收藏112次。@创建于:2022.05.27@修改于:2022.05.27文章目录1、过拟合与欠拟合2、欠拟合2.1 出现的原因2.2 解决的办法3、过拟合3.1 出现的原因3.2 解决的办法4. Early stopping5、Dropout6、L1 和 L2 正则化7、参考资料1、过拟合与欠拟合机器学习中模型的泛化能力强的模型才是好模型。对于训练好的模型:若在训练集表现差,不必说在测试集表现同样会很差,这可能是欠拟合导致;若模型在训练集表现非常好,却在测试集上差强人意,则这便是过拟合导致的。过拟合_svm过拟合怎么解决
文章浏览阅读102次。【代码】go上传文件。
文章浏览阅读1.5w次,点赞3次,收藏46次。一篇文章让你全面了解TDengine本文将从以下几个方面全面介绍TDengine。TDengine的基本介绍TDengine的发展历程TDengine的优势TDengine的适用场景TDengine的写入存储策略TDengine的特点TDengine的基本介绍一句话了解TDengineTDengine是一个高效的存储、查询、分析时序大数据的平台,专为物联网、车联网、工业互联网、运维监测等优化而设计。你可以像使用关系型数据库MySQL一样来使用它,简单又方便。为什么会有TDengin_tdengine
文章浏览阅读215次。 成为JavaGC专家Part II :如何监控Java垃圾回收机制 本文是成为Java GC专家系列文章的第二篇。在第一篇《深入浅出Java垃圾回收机制》中我们学习了不同GC算法的执行过程,GC是如何工作的,什么是新生代和老年代,你应该了解的JDK7中的5种GC类型,以及这5种类型对于应用性能的影响。 在本文中,我将解释JVM到底是如何执行垃圾回收处理..._成为javagc专家part ii — 如何监控java垃圾回收机制。
文章浏览阅读122次。文章目录python学习导航线一、seleniumpython-selenium二、python基础知识python的聊天室python学习导航线一、seleniumpython-selenium二、python基础知识python的聊天室_python点线导航
文章浏览阅读3.4k次,点赞4次,收藏22次。静态成员静态成员都是用static修饰,它的特点是不论创建多少个对象,程序都只创建一个静态成员。最主要的特点:共享什么是共享呢?例如:统计超市中所有商品数量的总和,商品数量的总和是随着每一个数量的变化而变化的,这是我们就可以用静态成员处理。(代码下面有写)静态成员又分为静态成员变量和静态成员函数。(一)静态成员变量特点:1、所有对象共享一份数据。 2、在编译阶段分配内存。 3、类内声明,类外初始化。#include<io..._静态成员变量
文章浏览阅读879次,点赞21次,收藏20次。1 网页简介:基于HTML+CSS+JavaScript 制作七夕情人节表白网页、生日祝福、七夕告白、 求婚、浪漫爱情3D相册、炫酷代码,快来制作一款高端的表白网页送(他/她)浪漫的告白,制作修改简单,可自行更换背景音乐,文字和图片即可使用等任意html编辑软件进行运行及修改编辑等操作)。_html 满屏爱心
文章浏览阅读492次,点赞9次,收藏5次。【代码】【易飞】易飞ERP自动审核程序功能。_易飞单据审批设置