java - 如何保存背景可绘制对象?
问题描述
我意识到,即使 Sharedprefs 保存了复选框状态,它也不会以这种方式保持可绘制资源背景……还有什么方法可以保存它?我希望它保持如下图所示。编辑:所以我的目标是,在选中复选框时,背景更改,因为 sharedprefs,选中状态被保存并退出应用程序,复选框保持选中状态,但复选框的背景返回到“未突出显示”没有可绘制背景的状态
CheckBox C1,C2,C3;
//Creating keys for sharedpreference
private static final String C1key = "C1_key";
private static final String C2key = "C2_key";
private static final String C3key = "C3_key";
SharedPreferences shp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anime);
//sharedpreference created with the name as anime
shp = getSharedPreferences("Anime",MODE_PRIVATE);
//This is just background gradient animation
ConstraintLayout constraintLayout = findViewById(R.id.layout_anime);
AnimationDrawable animationDrawable = (AnimationDrawable) constraintLayout.getBackground();
animationDrawable.setEnterFadeDuration(2000);
animationDrawable.setExitFadeDuration(4000);
animationDrawable.start();
//Initializing checkboxes
C1 = findViewById(R.id.C1);
C2 = findViewById(R.id.C2);
C3 = findViewById(R.id.C3);
//mapping checkbox and string for ease of use during sharedprefs
Map<String, CheckBox> checkBoxMap = new HashMap();
checkBoxMap.put(C1key,C1);
checkBoxMap.put(C2key,C2);
checkBoxMap.put(C3key,C3);
//loading initial values from sharedprefs, and also creating onCheckedChangeListeners from the map
loadInitialValues(checkBoxMap);
setupCheckedChangeListener(checkBoxMap);
}
public void loadInitialValues(Map<String, CheckBox> checkboxMap) {
for (Map.Entry<String, CheckBox> checkboxEntry: checkboxMap.entrySet()) {
boolean checked = shp.getBoolean(checkboxEntry.getKey(), false);
checkboxEntry.getValue().setChecked(checked);
}
}
public void setupCheckedChangeListener(Map<String, CheckBox> checkboxMap) { //for loop to cover all the checkboxes and keys in the map
for (final Map.Entry<String, CheckBox> checkboxEntry: checkboxMap.entrySet()) {
checkboxEntry.getValue().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final SharedPreferences.Editor editor = shp.edit();
editor.putBoolean(checkboxEntry.getKey(), isChecked);
editor.apply();
// this part is to turn the background of the checkbox to a specified drawable when its checked and when it isn't
if(checkboxEntry.getValue().isChecked()) //checkboxentry.getvalue().ischecked is to check whether specific checkboxes are in the checked state or not, Ex C1.ischecked() C2.ischecked() and so on
{
checkboxEntry.getValue().setBackgroundResource(R.drawable.cb_background);
}
else
{
checkboxEntry.getValue().setBackgroundResource(R.drawable.cb_background_default);
}
}
});
}
解决方案
感谢 Suehtam 帮助我解决这个问题,非常感谢!!!这是解决方案
private void loadInitialValues(Map<String, CheckBox> checkboxMap) {
for (Map.Entry<String, CheckBox> checkboxEntry: checkboxMap.entrySet()) {
boolean checked = shp.getBoolean(checkboxEntry.getKey(), false);
checkboxEntry.getValue().setChecked(checked);
if (checkboxEntry.getValue().isChecked()) {
checkboxEntry.getValue().setSelected(true);
checkboxEntry.getValue().setBackgroundResource(R.drawable.cb_background);
}else{
//If is not selected
checkboxEntry.getValue().setBackgroundResource(0);
}
}
}
private void setupCheckedChangeListener(Map<String, CheckBox> checkboxMap) {
for (final Map.Entry<String, CheckBox> checkboxEntry: checkboxMap.entrySet()) {
checkboxEntry.getValue().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final SharedPreferences.Editor editor = shp.edit();
editor.putBoolean(checkboxEntry.getKey(), isChecked);
if (checkboxEntry.getValue().isChecked()) {
checkboxEntry.getValue().setSelected(true);
checkboxEntry.getValue().setBackgroundResource(R.drawable.cb_background);
}else{
//If is not selected
checkboxEntry.getValue().setBackgroundResource(0);
}
editor.apply();
}
});
}
}
推荐阅读
- jar - 如何让 Ivy 将我的依赖项的依赖项捆绑到他们自己的 jar 中?
- python - 如何在具有相同名称的其他目录中使用现有文件写入/附加程序的结果?
- python - 使用 yamltodb 将 YAML 数据转换为数据框
- vue.js - 在 vscode 中使用 vue/nuxt linting 规则
- html - 在 CSS 转换完成之前,如何使按钮无法点击?
- javascript - 什么是使用 setTimeout 时的异步处理?
- html - 带有溢出文本的自动滚动?(没有可见的 scoll 条)
- c# - 导入json不解析数据
- flutter - 如何在颤动中保持访问相同视图的历史?
- java - 空手道 DSL 断言嵌套 json