Android studio—SQlite数据库注册登录
文章目录
- Android studio—SQlite数据库注册登录
- 创建用户类
- 创建数据库
- 注册写入数据库
- 登录读取数据库
- 效果展示
- 布局文件
- 注册页面
- 登录页面
首先展示一下效果:
创建用户类
创建一个用户类,自动补全Get、Set方法
public class User {
private int id;
private String name;
private String password;
public User(String name,String password){
super();
this.name = name;
this.password = password;
}
public int getId() {return id;}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{id ="+ id + ", name = "+ name +",password ="+password +"}";
}
}
创建数据库
创建数据库在我上一篇博客中有详细提及 有兴趣的朋友可以去看看,这里我们就简单介绍。
创建SQLite类继承SQLiteOpenHelper帮助类
写一个这个类的构造函数,参数为上下文context,所谓上下文就是这个类所在包的路径
指明上下文,数据库名,工厂默认空值,版本号默认从1开始
super(context,"db_test",null,1);
把数据库设置成可写入状态,当内存满后,会自动设置为只读模式
db = getReadableDatabase();
完整代码:
public class SQlite extends SQLiteOpenHelper {
private SQLiteDatabase db;
public SQlite(Context context){
super(context,"db_test",null,1);
db = getReadableDatabase();
}
完善数据表属性:ID、用户名、密码:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS user(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT," +
"password TEXT)");
}
完善数据表功能,由于我们目前只需要完成注册功能,所以只需要添加数据库的增加数据功能即可:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS user");
onCreate(db);
}
public void add(String name,String password ){
db.execSQL("INSERT INTO user(name,password)VALUES(?,?)",new Object[]{name,password});
}
public ArrayList<User> getAllDATA(){
ArrayList<User> list = new ArrayList<User>();
Cursor cursor = db.query("user",null,null,null,null,null,"name DESC");
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
list.add(new User(name,password));
}
return list;
}
}
name DESC为排序方式,把读取的数据放入list例表中
注册写入数据库
创建一个Register类,完善控件定义,和取消事件触发,我们不多说,随后会贴到完整代码上。
重点讲注册按钮完成对数据库的写入:
String name = username.getText().toString().trim();
String password = userpassword.getText().toString().trim(); //获取输入的用户名和密码
mSQlite = new SQlite(Register.this);
==mSQlite = new SQlite(Register.this);==实例化mSQLite,用于登录数据验证
该方法在onCreate方法内实现。
完整代码:
public class Register extends AppCompatActivity {
private SQlite mSQlite;
private EditText username;
private EditText userpassword;
private Button reday;
private Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
reday = findViewById(R.id.reday);
back = findViewById(R.id.back);
username = findViewById(R.id.userName);
userpassword =findViewById( R.id.userpassword);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Register.this,MainActivity.class);
startActivity(intent);
finish();
}
});
reday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = username.getText().toString().trim();
String password = userpassword.getText().toString().trim();
if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(password)){
mSQlite.add(name,password);
Intent intent1 = new Intent(Register.this,MainActivity.class);
startActivity(intent1);
finish();
Toast.makeText(Register.this,"注册成功",Toast.LENGTH_SHORT).show();
}else {Toast.makeText(Register.this,"信息不完备,注册失败",Toast.LENGTH_SHORT).show();}
}
});
mSQlite = new SQlite(Register.this);
}
}
登录读取数据库
控件的完善定义,和注册跳转按钮事件不多说,随后会贴在完整代码上。
重点讲如何验证数据库,完成注册登录功能。
先读取用户输入的信息:
String name = username.getText().toString().trim();
String password = userpassword.getText().toString().trim();
先验证输入是否为空:
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {}
若不为空,进行判断是否与数据库数据匹配,如果匹配正确,则输出ture,跳出循环,进行登录操作,匹配不正确,则输出flase,提示账号或密码错误。通过for()循环查询数据库是否有用户数据:
ArrayList<User> data = mSQlite.getAllDATA();
boolean userdata = false;
for (int i = 0; i < data.size(); i++) {
User user = data.get(i); //可存储账号数量
if (name.equals(user.getName())) {
userdata = true;
break;
} else {
userdata = false;
}
}
完整代码:
public class MainActivity extends AppCompatActivity {
private SQlite mSQlite;
private EditText username;
private EditText userpassword;
private Button login;
private Button register;
private User view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = findViewById(R.id.login);
register = findViewById(R.id.register);
username = findViewById(R.id.userName);
userpassword = findViewById(R.id.userpassword);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent5 = new Intent(MainActivity.this, Register.class);
startActivity(intent5);
finish();
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = username.getText().toString().trim();
String password = userpassword.getText().toString().trim();
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
ArrayList<User> data = mSQlite.getAllDATA();
boolean user = false;
for (int i = 0; i < data.size(); i++) {
User userdata= data.get(i); //可存储账号数量
if (name.equals(user.getName()) && password.equals(user.getPassword())) {
userdata = true;
break;
} else {
userdata = false;
}
}
if (userdata) {
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, Main2.class);
intent.putExtra("username",name);
intent.putExtra("password",password); //展示账号密码功能
startActivity(intent);
finish();
} else {
Toast.makeText(MainActivity.this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
}
}
});
mSQlite = new SQlite(MainActivity.this);
}
}
为了验证我们的登陆数据是否正确,我们还要新建一个Main2Acitity。 具体方法参考我之前的博客:初级登录页面里的展示账号密码功能
TextView textView = findViewById(R.id.textT);
Intent intent =getIntent();
String name =intent.getStringExtra("username");
String password =intent.getStringExtra("password");
textView.setText("登录数据:"+"\n"+"用户名:"+name+"\n"+"密码:"+password);
效果展示
布局文件
注册页面和登录页面的布局文件其实大致相同。
注册页面
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Register">
<LinearLayout
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="60dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="用户名:"
android:layout_marginTop="30dp"
android:textSize="25sp"
android:textColor="#000000"
android:layout_weight="1"/>
<EditText
android:id="@+id/userName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:textColor="#2196F3"
android:layout_weight="2"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="25sp"
android:layout_marginTop="30dp"
android:textColor="#000000"
android:layout_weight="1"/>
<EditText
android:id="@+id/userpassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:hint="请输入密码"
android:textSize="20sp"
android:textColor="#2196F3"
android:layout_weight="2"
android:inputType="textWebPassword"/>
</LinearLayout>
</LinearLayout>
<Button
android:textColor="#Ef4000"
android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textSize="25sp"
android:textAllCaps="false"
android:text="确认"
android:id="@+id/reday"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:textColor="#Ef4000"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textSize="25sp"
android:textAllCaps="false"
android:text="取消"
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
登录页面
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="用户名:"
android:layout_marginTop="30dp"
android:textSize="25sp"
android:textColor="#000000"
android:layout_weight="1"/>
<EditText
android:id="@+id/userName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:textColor="#2196F3"
android:layout_weight="2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="25sp"
android:layout_marginTop="30dp"
android:textColor="#000000"
android:layout_weight="1"/>
<EditText
android:id="@+id/userpassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:hint="请输入密码"
android:textSize="20sp"
android:textColor="#2196F3"
android:layout_weight="2"
android:inputType="textWebPassword"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textAllCaps="false"
android:textColor="#EE4000"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:textSize="25sp" />
<Button
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:textSize="25sp"
android:textColor="#EE4000"
android:textAllCaps="false"
android:text="注册"
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>