【发布时间】:2017-06-15 17:03:39
【问题描述】:
我是 Android 开发新手,我想知道如何在本地保存复选框状态以供用户重新打开应用程序时使用,并且复选框保持红色、黄色或绿色。我的代码在一个片段中,每次打开应用程序时,它都是我设置的默认红色。我有自定义复选框,它们是圆形的,当用户单击复选框时,它会保持选中状态,但会更改颜色。我进行了很多研究,并且尝试了 SharedPreferences,但它不起作用,我需要知道让用户单击复选框并查看它是红色、黄色还是绿色的逻辑是什么。我所看到的是一个复选框只被选中一次,如果它被选中,它会保存状态,但我有复选框被多次选中并且每次都改变颜色。这是我用于复选框的方法的代码:
public int checked(CheckBox checkBox, int count){
if (checkBox.isChecked()){
count++;
} else if (!checkBox.isChecked()){
checkBox.setChecked(true);
count++;
}
if (count == 1){
checkBox.setButtonDrawable(R.drawable.custom_yellow_checkbox);
}
if (count == 2){
checkBox.setButtonDrawable(R.drawable.custom_green_checkbox);
}
if (count > 2){
count = 0;
checkBox.setButtonDrawable(R.drawable.custom_red_checkbox);
}
return count;
}
这里是checkbox被选中时的方法本身。
chkStart.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
countStart = checked(chkStart, countStart);
}
});
所以,我想知道是否有一种方法可以将复选框状态保存在选中的方法中,或者有其他方法来实现这一点。如果有人知道为复选框保存状态并让它们保持一定的可绘制状态并检查 0、1 或 2 次,我将不胜感激。提前致谢!
【问题讨论】:
标签: android checkbox savestate