android 如何实现勾选checkbox后,文本框中显示之前写入到SQLite数据库中的内容
用CompoundButton类的复选按钮,使用onCheckedChanged 方法实现,下面是一个例子可以代入执行:
CheckBox checkBox = findViewById(R.id.checkbox);
EditText editText = findViewById(R.id.edit_text);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editText.setEnabled(isChecked);
if (isChecked) {
String previousContent = getDataFromSQLite();
editText.setText(previousContent);
}
else {
editText.setText("");
}
}
});
private String getDataFromSQLite() {
return "save result";
}
//如果你向获取sqlite的内容可以创建SQLiteOpenHelper 的子类,并使用onCreate :
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "my_database.db";
private static final int DATABASE_VERSION = 1;
public MyDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableSql = "CREATE TABLE my_table (content TEXT)";
db.execSQL(createTableSql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MyDatabaseHelper dbHelper = new MyDatabaseHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] projection = { "content" };
Cursor cursor = db.query("my_table", projection, null, null, null, null, null);
if (cursor.moveToFirst()) {
String previousContent = cursor.getString(0);
}
```
该回答引用chatgpt:
您可以使用以下步骤在Android中实现勾选CheckBox后,文本框中显示之前写入到SQLite数据库中的内容:
在您的布局文件中添加一个CheckBox和一个文本框:
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示之前写入的内容" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入内容" />
在您的Activity中获取CheckBox和文本框的实例,并将之前写入到SQLite数据库中的内容读取出来:
CheckBox checkBox = findViewById(R.id.checkBox);
EditText editText = findViewById(R.id.editText);
// 读取之前写入到SQLite数据库中的内容
String savedText = getSavedTextFromDatabase();
editText.setText(savedText);
注意,getSavedTextFromDatabase()是一个您需要自己实现的方法,用于从SQLite数据库中读取之前保存的文本内容。
实现CheckBox的监听器,在CheckBox被勾选时显示之前保存的文本内容:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
String savedText = getSavedTextFromDatabase();
editText.setText(savedText);
} else {
editText.setText("");
}
}
});
在CheckBox被勾选时,调用getSavedTextFromDatabase()方法读取之前保存的文本内容,并将其显示在文本框中。如果CheckBox被取消勾选,文本框将清空。
我们通过一个模拟点赞的例子, 来说明点击事件
首先来个颜色选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#0f0" android:state_checked="true"/>
<!--设置为透明, 未选中时,透明色覆盖-->
<item android:color="#0000"/>
</selector>
activity_main.xml
注意, 需要将button置为空
并且将图片放在控件顶部
<!--
checked: 是否被选中
android:button="@drawable/cb_sel" 可以被替换掉
可是使用@null将原生控件去掉
-->
<CheckBox
android:layout_centerInParent="true"
android:id="@+id/main_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:checked="true"
android:drawableTop="@mipmap/ic_launcher"
android:text="复选框" />
给它着色, 可以使用 android:drawableTint=”@color/cb_color_sel”在xml中设置, 但是它是API23以后才可以使用 目前(2016-8-26)几乎不能用, 所以我们要找一个兼容的办法, 看MainActivity.java
MainActivity.java
CheckBox checkBox = (CheckBox) findViewById(R.id.main_cb);
//checkBox.setCompoundDrawableTintList();//API23之后可用
//获取到四周的drawable, 分为左上中下
Drawable[] drawables = checkBox.getCompoundDrawables();
//拿到顶部 顺序: 左上右下
Drawable top = drawables[1];
//top.setTint();最低版本21, 使用兼容包, 来兼容低版本手机
// 参数: 1, 给谁着色 2,获取颜色列表
ColorStateList list = getResources().getColorStateList(R.color.cb_color_sel);
DrawableCompat.setTintList(top,
list);
DrawableCompat.setTintMode(top, PorterDuff.Mode.SRC_ATOP);
//设置监听
checkBox.setOnCheckedChangeListener(this);
checkBox.setText("+" + count);
事件监听方法
//点击事件方法
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setText("+" + (count + (isChecked ? 1 : 0)));
}
效果图:
注意:
1, 一般情况下, 后面带有 ~Compat的, 为支持包;
2, checkBox.getCompoundDrawables();返回的是该控件在四周的drawable, 一个四个, 0 - 3 (包含) , 顺序是, 左上右下