帮我写个android的登陆注册页面,使用sqlite数据库
时间: 2023-11-27 15:53:26 浏览: 85
android使用sqlite的登录注册例子
好的,下面是一个简单的 Android 登录注册页面的示例,使用 SQLite 数据库进行用户信息的保存和验证。
首先,在 Android Studio 中创建一个新项目,并添加以下依赖项到 app/build.gradle 文件中:
```
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.sqlite:sqlite:2.0.1'
}
```
接下来,在 res/layout 文件夹中创建两个布局文件:activity_login.xml 和 activity_register.xml。这些布局文件将包含登录和注册表单。
activity_login.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"/>
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"/>
<TextView
android:id="@+id/txt_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Don't have an account? Register here."/>
</LinearLayout>
```
activity_register.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"/>
<EditText
android:id="@+id/edit_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"/>
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"/>
</LinearLayout>
```
接下来,创建一个名为 DatabaseHelper 的帮助程序类,该类将处理 SQLite 数据库的创建和版本控制:
```java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "users.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_USERS = "users";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_PASSWORD = "password";
private static final String CREATE_TABLE_USERS = "CREATE TABLE " +
TABLE_USERS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_EMAIL + " TEXT, " +
COLUMN_PASSWORD + " TEXT" +
")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_USERS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
onCreate(db);
}
public boolean registerUser(String name, String email, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_EMAIL, email);
values.put(COLUMN_PASSWORD, password);
long result = db.insert(TABLE_USERS, null, values);
return result != -1;
}
public boolean loginUser(String email, String password) {
SQLiteDatabase db = this.getReadableDatabase();
String selection = COLUMN_EMAIL + " = ? AND " + COLUMN_PASSWORD + " = ?";
String[] selectionArgs = {email, password};
Cursor cursor = db.query(
TABLE_USERS,
null,
selection,
selectionArgs,
null,
null,
null
);
int count = cursor.getCount();
cursor.close();
return count > 0;
}
}
```
在 LoginActivity 中,我们将使用 SQLite 数据库验证用户是否存在,并跳转到 MainActivity:
```java
public class LoginActivity extends AppCompatActivity {
private EditText mEmailEditText;
private EditText mPasswordEditText;
private DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mEmailEditText = findViewById(R.id.edit_email);
mPasswordEditText = findViewById(R.id.edit_password);
Button loginButton = findViewById(R.id.btn_login);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmailEditText.getText().toString().trim();
String password = mPasswordEditText.getText().toString().trim();
if (email.isEmpty() || password.isEmpty()) {
Toast.makeText(LoginActivity.this, "Please enter email and password", Toast.LENGTH_SHORT).show();
} else {
if (mDatabaseHelper.loginUser(email, password)) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "Invalid email or password", Toast.LENGTH_SHORT).show();
}
}
}
});
TextView registerTextView = findViewById(R.id.txt_register);
registerTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
mDatabaseHelper = new DatabaseHelper(this);
}
}
```
在 RegisterActivity 中,我们将使用 SQLite 数据库注册新用户:
```java
public class RegisterActivity extends AppCompatActivity {
private EditText mNameEditText;
private EditText mEmailEditText;
private EditText mPasswordEditText;
private DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mNameEditText = findViewById(R.id.edit_name);
mEmailEditText = findViewById(R.id.edit_email);
mPasswordEditText = findViewById(R.id.edit_password);
Button registerButton = findViewById(R.id.btn_register);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mNameEditText.getText().toString().trim();
String email = mEmailEditText.getText().toString().trim();
String password = mPasswordEditText.getText().toString().trim();
if (name.isEmpty() || email.isEmpty() || password.isEmpty()) {
Toast.makeText(RegisterActivity.this, "Please enter name, email and password", Toast.LENGTH_SHORT).show();
} else {
if (mDatabaseHelper.registerUser(name, email, password)) {
Toast.makeText(RegisterActivity.this, "Registration successful", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(RegisterActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
}
}
}
});
mDatabaseHelper = new DatabaseHelper(this);
}
}
```
这样,我们就完成了一个简单的 Android 登录注册页面,使用 SQLite 数据库进行用户信息的保存和验证。
阅读全文