首页 > 解决方案 > 如何保存背景可绘制对象?

问题描述

我意识到,即使 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);
                    }

                }
            });
        }

    

在此处输入图像描述

标签: javaandroidandroid-studio

解决方案


感谢 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();
    
                    }
                });
            }
    
        }

推荐阅读