根据以下要求写代码:创建界面布局文件,包括文本框、按钮等控件; 在Activity中获取各个控件的引用,并设置事件监听器; 实现合法性检查方法,判断姓名是否填写; 使用SQLiteOpenHelper建立数据库连接或打开已有数据库; 在数据库中创建名为"peopleinfo"的表,包括id(自动增长)、name、gender、major和hobby字段; 实现ContentProvider,其中包括必须实现的query()、insert()、update()和delete()方法; ContentProvider的authorities字符串设为mc.lab_5_1.peopleprovider; 在插入数据时使用ContentValues将信息添加到数据库中; 在提交成功后提示用户,并清空界面上的输入数据。
时间: 2024-03-11 22:49:12 浏览: 63
Android 在程序中动态添加 View 布局或控件
好的,根据您的要求,以下是相应的代码实现:
1. 界面布局文件(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">
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" />
<RadioGroup
android:id="@+id/radio_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/radio_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<EditText
android:id="@+id/edit_major"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入专业" />
<EditText
android:id="@+id/edit_hobby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入爱好" />
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交" />
</LinearLayout>
```
2. Activity中获取控件引用并设置事件监听器(MainActivity.java):
```java
public class MainActivity extends AppCompatActivity {
private EditText mNameEditText;
private RadioGroup mGenderRadioGroup;
private EditText mMajorEditText;
private EditText mHobbyEditText;
private Button mSubmitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameEditText = findViewById(R.id.edit_name);
mGenderRadioGroup = findViewById(R.id.radio_gender);
mMajorEditText = findViewById(R.id.edit_major);
mHobbyEditText = findViewById(R.id.edit_hobby);
mSubmitButton = findViewById(R.id.btn_submit);
mSubmitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isValid()) {
insertData();
clearInputs();
Toast.makeText(MainActivity.this, "提交成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "请输入姓名!", Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean isValid() {
return !TextUtils.isEmpty(mNameEditText.getText().toString());
}
private void insertData() {
// TODO: 插入数据到数据库中
}
private void clearInputs() {
mNameEditText.setText("");
mGenderRadioGroup.clearCheck();
mMajorEditText.setText("");
mHobbyEditText.setText("");
}
}
```
3. 使用SQLiteOpenHelper建立数据库连接(DatabaseHelper.java):
```java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "people.db";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE peopleinfo ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "name TEXT,"
+ "gender INTEGER,"
+ "major TEXT,"
+ "hobby TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 不需要实现
}
}
```
4. 实现ContentProvider(PeopleProvider.java):
```java
public class PeopleProvider extends ContentProvider {
private static final String AUTHORITY = "mc.lab_5_1.peopleprovider";
private static final String TABLE_NAME = "peopleinfo";
private static final int MATCH_ALL = 1;
private static final int MATCH_ONE = 2;
private DatabaseHelper mDatabaseHelper;
private UriMatcher mUriMatcher;
@Override
public boolean onCreate() {
mDatabaseHelper = new DatabaseHelper(getContext());
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mUriMatcher.addURI(AUTHORITY, TABLE_NAME, MATCH_ALL);
mUriMatcher.addURI(AUTHORITY, TABLE_NAME + "/#", MATCH_ONE);
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
switch (mUriMatcher.match(uri)) {
case MATCH_ALL:
return db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
case MATCH_ONE:
long id = ContentUris.parseId(uri);
return db.query(TABLE_NAME, projection, "_id=?", new String[]{String.valueOf(id)}, null, null, sortOrder);
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
switch (mUriMatcher.match(uri)) {
case MATCH_ALL:
return ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + AUTHORITY + "/" + TABLE_NAME;
case MATCH_ONE:
return ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + AUTHORITY + "/" + TABLE_NAME;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
long id = db.insert(TABLE_NAME, null, values);
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(uri, id);
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int count = db.delete(TABLE_NAME, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int count = db.update(TABLE_NAME, values, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
```
5. 在AndroidManifest.xml中注册ContentProvider:
```xml
<provider
android:name=".PeopleProvider"
android:authorities="mc.lab_5_1.peopleprovider"
android:exported="false" />
```
至此,根据您的要求,相关的代码实现已经完成。
阅读全文