帮我写个android的登陆注册,使用sqlite数据库
时间: 2023-09-14 19:09:10 浏览: 99
Android Studio连接SQLite数据库的登录注册实现
5星 · 资源好评率100%
好的,以下是一个简单的Android登陆注册应用示例,使用SQLite数据库:
1. 创建一个名为“LoginRegistration”的Android项目。
2. 在res/layout目录下创建两个布局文件:activity_login.xml和activity_register.xml,分别用于登录和注册页面的UI设计。
3. 在res/menu目录下创建一个menu.xml文件,用于添加菜单项。
4. 在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
```
5. 创建一个名为“DatabaseHelper”的Java类,用于创建和管理SQLite数据库。
```java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "LoginRegistration.db";
public static final String TABLE_NAME = "user_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "USERNAME";
public static final String COL_3 = "PASSWORD";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME TEXT, PASSWORD TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, username);
contentValues.put(COL_3, password);
long result = db.insert(TABLE_NAME, null, contentValues);
return result != -1;
}
public boolean checkUser(String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
String[] columns = { COL_1 };
String selection = COL_2 + " = ?" + " AND " + COL_3 + " = ?";
String[] selectionArgs = { username, password };
Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
int count = cursor.getCount();
cursor.close();
return count > 0;
}
}
```
6. 在LoginActivity中实现登录功能。在onCreate方法中,获取用户名和密码输入框的实例,并设置登录按钮的点击事件:
```java
public class LoginActivity extends AppCompatActivity {
EditText usernameEditText, passwordEditText;
Button loginButton;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
databaseHelper = new DatabaseHelper(this);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
if (databaseHelper.checkUser(username, password)) {
Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "Invalid username or password", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
7. 在RegisterActivity中实现注册功能。在onCreate方法中,获取用户名、密码和确认密码输入框的实例,并设置注册按钮的点击事件:
```java
public class RegisterActivity extends AppCompatActivity {
EditText usernameEditText, passwordEditText, confirmPasswordEditText;
Button registerButton;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
confirmPasswordEditText = findViewById(R.id.confirmPasswordEditText);
registerButton = findViewById(R.id.registerButton);
databaseHelper = new DatabaseHelper(this);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
String confirmPassword = confirmPasswordEditText.getText().toString();
if (password.equals(confirmPassword)) {
boolean isInserted = databaseHelper.insertData(username, password);
if (isInserted) {
Toast.makeText(RegisterActivity.this, "Registration successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
} else {
Toast.makeText(RegisterActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(RegisterActivity.this, "Passwords do not match", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
8. 在MainActivity中添加菜单项,用于退出应用:
```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.exit:
finish();
System.exit(0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
```
9. 运行应用,测试登录和注册功能。
以上是一个简单的Android登陆注册应用示例,使用SQLite数据库。
阅读全文