android 实现增删改查和登陆界面
时间: 2023-12-25 09:04:21 浏览: 47
实现增删改查功能需要使用 Android 中的 SQLite 数据库。下面是一个简单的示例:
### 创建数据库
在 Android 中创建和管理 SQLite 数据库需要使用 `SQLiteOpenHelper` 类。首先需要定义一个继承自该类的 helper 类:
```java
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydb.db";
private static final int DATABASE_VERSION = 1;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS contacts ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "name TEXT,"
+ "phone TEXT,"
+ "email TEXT"
+ ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
```
在 `onCreate()` 方法中创建一个名为 `contacts` 的表,包括 `id`、`name`、`phone` 和 `email` 四个字段。`id` 为主键,自增长。
### 增加数据
在 Activity 中使用 `ContentValues` 对象向数据库中插入数据:
```java
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "Tom");
values.put("phone", "123456789");
values.put("email", "tom@example.com");
db.insert("contacts", null, values);
```
### 删除数据
使用 `delete()` 方法删除数据:
```java
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("contacts", "name = ?", new String[] {"Tom"});
```
### 更新数据
使用 `update()` 方法更新数据:
```java
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("phone", "987654321");
db.update("contacts", values, "name = ?", new String[] {"Tom"});
```
### 查询数据
使用 `query()` 方法查询数据:
```java
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("contacts", null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String email = cursor.getString(cursor.getColumnIndex("email"));
Log.d(TAG, "id: " + id + ", name: " + name + ", phone: " + phone + ", email: " + email);
}
cursor.close();
```
### 登陆界面
登陆界面可以使用 Android 中的 `EditText`、`Button`、`TextView` 等控件完成。下面是一个简单的示例:
```xml
<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:inputType="textPassword"
android:hint="密码"/>
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
```
在 Activity 中为按钮设置点击事件:
```java
Button loginButton = findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText usernameEditText = findViewById(R.id.username);
EditText passwordEditText = findViewById(R.id.password);
TextView resultTextView = findViewById(R.id.result);
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
if ("admin".equals(username) && "123456".equals(password)) {
resultTextView.setText("登陆成功");
} else {
resultTextView.setText("用户名或密码错误");
}
}
});
```