![](https://i-blog.csdnimg.cn/direct/a784107bb5cc4a88923a1a492c121f96.png)
MySQLiteOpenHelper
package com.example.addressbook;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import com.example.addressbook.bean.User;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static final String DB_NAME="MySQLite.db";
private static final String create_users="create table users(name varchar(32),password varchar(32))";
public MySQLiteOpenHelper(@Nullable Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(create_users);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long U_register(User u){
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name",u.getName());
cv.put("password",u.getPassword());
long users = db.insert("users",null,cv);
return users;
}
public int U_login(String name,String password){
SQLiteDatabase db = getWritableDatabase();
boolean result=false;
Cursor users = db.query("users", null, "name like ?", new String[]{name}, null, null, null);
if(users!=null){
while(users.moveToNext()){
String password1 = users.getString(1);
result = password1.equals(password);
String ad = users.getString(0);
String T = "admin";
if(result && (ad.equals(T))){
return 1;
}
return 2;
}
}
return 3;
}
}
bean封装类User
package com.example.addressbook.bean;
public class User {
private String name;
private String password;
public User(){
}
public User(String name,String password){
this.name = name;
this.password = password;
}
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;
}
}
MainActivity.java
package com.example.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.addressbook.Util.ToastUtil;
public class MainActivity extends AppCompatActivity {
private Button login,reset;
private TextView register;
private EditText edName,edPassword;
private MySQLiteOpenHelper mySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
init();
}
public void init(){
login = findViewById(R.id.login);
register = findViewById(R.id.register);
edName = findViewById(R.id.edName);
edPassword = findViewById(R.id.edPassword);
reset = findViewById(R.id.reset);
}
public void login(View view) {
String name = edName.getText().toString().trim();
String password = edPassword.getText().toString().trim();
int login = mySQLiteOpenHelper.U_login(name,password);
if(login == 1){
ToastUtil.toastShort(this,"登录成功!欢迎管理员!");
Intent intent = new Intent(this,ManagerAbActivity.class);
startActivity(intent);
}else if(login == 2){
ToastUtil.toastShort(this,"登录成功!欢迎会员!");
Intent intent = new Intent(this,UserAbActivity.class);
startActivity(intent);
}else{
ToastUtil.toastShort(this,"登录失败");
}
}
public void register(View view) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
}
public void reset(View view) {
edName.setText("");
edPassword.setText("");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@drawable/bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal"
android:layout_marginTop="30dp"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_gravity="center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/title"
android:layout_marginTop="10dp"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:text="用户登录"
android:textSize="32sp"
android:textColor="@color/pink"
android:textStyle="bold"/>
<TextView
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户注册"
android:textSize="32sp"
android:textColor="@color/lblue"
android:textStyle="bold"
android:onClick="register"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="账号:"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="@color/grey"/>
<EditText
android:id="@+id/edName"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:background="@drawable/edittext_border"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="30dp">
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textStyle="bold"
android:text="密码:"
android:textSize="30sp"
android:textColor="@color/grey"/>
<EditText
android:id="@+id/edPassword"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:background="@drawable/edittext_border"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="50dp"
android:gravity="center">
<Button
android:id="@+id/login"
android:layout_width="160dp"
android:layout_height="60dp"
android:text="登录"
android:textColor="@color/white"
android:textSize="28sp"
android:background="@drawable/round_button"
android:textStyle="bold"
android:onClick="login"/>
<Button
android:id="@+id/reset"
android:layout_width="160dp"
android:layout_height="60dp"
android:textColor="@color/white"
android:text="重置"
android:textSize="28sp"
android:background="@drawable/round_button"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:onClick="reset"/>
</LinearLayout>
</LinearLayout>
RegisterActivity
package com.example.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.addressbook.Util.ToastUtil;
import com.example.addressbook.bean.User;
public class RegisterActivity extends AppCompatActivity {
private EditText name1,password1;
private TextView l;
private TextView register;
private Button reset;
private MySQLiteOpenHelper mySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
init();
}
public void init(){
name1 = findViewById(R.id.edName);
password1 = findViewById(R.id.edPassword);
register = findViewById(R.id.register);
l = findViewById(R.id.L);
reset = findViewById(R.id.reset);
}
public void register(View view) {
String name = name1.getText().toString().trim();
String password = password1.getText().toString().trim();
User u = new User(name,password);
long r = mySQLiteOpenHelper.U_register(u);
if(r!=-1){
ToastUtil.toastShort(this,"注册成功");
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}else{
ToastUtil.toastShort(this,"注册失败");
}
}
public void Log(View view) {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
public void reset(View view) {
name1.setText("");
password1.setText("");
}
}
activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".RegisterActivity"
android:orientation="vertical"
android:background="@drawable/bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal"
android:layout_marginTop="30dp"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_gravity="center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/title"
android:layout_marginTop="10dp"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/L"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:text="用户登录"
android:textSize="32sp"
android:textColor="@color/lblue"
android:textStyle="bold"
android:onClick="Log"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户注册"
android:textSize="32sp"
android:textColor="@color/pink"
android:textStyle="bold"
android:onClick="register"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="账号:"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="@color/grey"/>
<EditText
android:id="@+id/edName"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:background="@drawable/edittext_border"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="30dp">
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textStyle="bold"
android:text="密码:"
android:textSize="30sp"
android:textColor="@color/grey"/>
<EditText
android:id="@+id/edPassword"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:background="@drawable/edittext_border"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="50dp"
android:gravity="center">
<Button
android:id="@+id/register"
android:layout_width="160dp"
android:layout_height="60dp"
android:text="确定注册"
android:textSize="28sp"
android:textColor="@color/white"
android:background="@drawable/round_button"
android:textStyle="bold"
android:onClick="register"/>
<Button
android:id="@+id/reset"
android:layout_width="160dp"
android:layout_height="60dp"
android:textColor="@color/white"
android:text="重置"
android:textSize="28sp"
android:background="@drawable/round_button"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:onClick="reset"/>
</LinearLayout>
</LinearLayout>