【问题标题】:Android How to save the state of checkboxes that are 3 different checked statesAndroid 如何保存 3 种不同选中状态的复选框的状态
【发布时间】: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


    【解决方案1】:

    实际上,即使应用程序关闭,SharedPreferences 也是保持 CheckBoxes 状态的好选择。只需保存正确的 key:value duple 即可使其正常工作:我将使用 checkboxId 作为键,将您使用的 count 变量用作值。例如:

        SharedPreferences mSharedPreferences = this.getSharedPreferences("preferences_key", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor;
    
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        editor = mSharedPreferences.edit();
    
        chkStart.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                countStart = checked(chkStart, countStart);
                editor.putInt("checkStartId", countStart);
                editor.apply();
            }
        });
    }
    
        @Override
        protected void onResume() {
            super.onResume();
            int value = mSharedPreferences.getInt("checkStartId", 0) // 0 is the default value, in case there is not value saved
        }
    

    在应用启动时设置 CheckBox 的状态(例如在方法 onResume() 上)从 SharedPreferences 获取特定复选框的计数值并设置正确的颜色。

    【讨论】:

    • 你能告诉我如何获取 onResume() 中的计数值吗?是 editor.getInt(something) 还是获取计数值的方法。谢谢!
    • 我还得到了上下文变量的空指针异常。我应该如何初始化上下文,这样我就不会出现空指针异常?
    • 我已经更新了我的答案,我没有测试它,但我认为它会起作用,以它为例! :)
    • 你使用变量值吗?如果是这样,我怎么知道复选框的颜色是什么?
    • 有没有办法只在片段中执行此操作,或者我必须有一个活动才能使用共享首选项?我宁愿没有活动,因为我拥有的整个设置都是片段,但如果我必须这样做,我会的,但希望我可以在片段中完成。请让我知道我是否可以在一个片段中做到这一点。
    猜你喜欢
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2011-12-31
    相关资源
    最近更新 更多