写个android的登陆注册页面,使用sqlite数据库
时间: 2023-11-27 19:53:20 浏览: 97
好的,下面是一个简单的 Android 登录注册页面的示例代码,使用 SQLite 数据库:
首先是布局文件 `activity_main.xml`:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 登录表单 -->
<LinearLayout
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="vertical"
android:visibility="visible">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<TextView
android:id="@+id/register_link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="没有账号?点击注册"
android:textColor="#00f" />
</LinearLayout>
<!-- 注册表单 -->
<LinearLayout
android:id="@+id/register_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="vertical"
android:visibility="gone">
<EditText
android:id="@+id/register_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/register_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword" />
<Button
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
<TextView
android:id="@+id/login_link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="已有账号?点击登录"
android:textColor="#00f" />
</LinearLayout>
</LinearLayout>
```
其中包含两个表单,一个用于登录,一个用于注册。
接下来是 MainActivity 的代码:
```java
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText mUsernameView;
private EditText mPasswordView;
private EditText mRegisterUsernameView;
private EditText mRegisterPasswordView;
private LinearLayout mLoginForm;
private LinearLayout mRegisterForm;
private SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsernameView = findViewById(R.id.username);
mPasswordView = findViewById(R.id.password);
mRegisterUsernameView = findViewById(R.id.register_username);
mRegisterPasswordView = findViewById(R.id.register_password);
mLoginForm = findViewById(R.id.login_form);
mRegisterForm = findViewById(R.id.register_form);
Button loginButton = findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
attemptLogin();
}
});
Button registerButton = findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
attemptRegister();
}
});
TextView registerLink = findViewById(R.id.register_link);
registerLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showRegisterForm();
}
});
TextView loginLink = findViewById(R.id.login_link);
loginLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showLoginForm();
}
});
mDatabase = new DatabaseHelper(this).getWritableDatabase();
}
private void attemptLogin() {
String username = mUsernameView.getText().toString();
String password = mPasswordView.getText().toString();
Cursor cursor = mDatabase.query(
DatabaseHelper.TABLE_USERS,
null,
DatabaseHelper.COLUMN_USERNAME + " = ? AND " + DatabaseHelper.COLUMN_PASSWORD + " = ?",
new String[] {username, password},
null, null, null);
if (cursor.moveToFirst()) {
// 登录成功
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
} else {
// 登录失败
Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
cursor.close();
}
private void attemptRegister() {
String username = mRegisterUsernameView.getText().toString();
String password = mRegisterPasswordView.getText().toString();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
Cursor cursor = mDatabase.query(
DatabaseHelper.TABLE_USERS,
null,
DatabaseHelper.COLUMN_USERNAME + " = ?",
new String[] {username},
null, null, null);
if (cursor.moveToFirst()) {
// 用户名已存在
Toast.makeText(this, "用户名已存在", Toast.LENGTH_SHORT).show();
} else {
// 注册新用户
ContentValues values = new ContentValues();
values.put(DatabaseHelper.COLUMN_USERNAME, username);
values.put(DatabaseHelper.COLUMN_PASSWORD, password);
mDatabase.insert(DatabaseHelper.TABLE_USERS, null, values);
// 注册成功,自动登录
mUsernameView.setText(username);
mPasswordView.setText(password);
attemptLogin();
}
cursor.close();
}
private void showLoginForm() {
mLoginForm.setVisibility(View.VISIBLE);
mRegisterForm.setVisibility(View.GONE);
}
private void showRegisterForm() {
mLoginForm.setVisibility(View.GONE);
mRegisterForm.setVisibility(View.VISIBLE);
}
@Override
protected void onDestroy() {
super.onDestroy();
mDatabase.close();
}
}
```
其中 `DatabaseHelper` 是一个辅助类,用于创建和升级数据库:
```java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "myapp.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_USERS = "users";
public static final String COLUMN_USERNAME = "username";
public static final String COLUMN_PASSWORD = "password";
private static final String SQL_CREATE_USERS_TABLE =
"CREATE TABLE " + TABLE_USERS + " (" +
COLUMN_USERNAME + " TEXT PRIMARY KEY," +
COLUMN_PASSWORD + " TEXT)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_USERS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 升级数据库的代码
}
}
```
以上就是一个简单的 Android 登录注册页面的示例代码,使用 SQLite 数据库。
阅读全文