SharedPreferences不保存复选框状态

时间:2014-09-26 09:54:07

标签: android sharedpreferences android-checkbox

我正在使用共享偏好开发任务列表应用程序。但它不会保存复选框状态。我想在用户点击复选框时保存。用户退出应用程序并再次看到它已经更新状态。如何保存复选框状态? 这是我的代码(已经修复和工作)

public class TaskList extends Activity {
    CheckBox cb1, cb2, cb3, cb4;
    Boolean checked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_tasks);
        super.onCreate(savedInstanceState);
        findID();

        cb1 = (CheckBox) findViewById(R.id.checkBox1);
        boolean isChecked = getBooleanFromPreferences("isChecked");
        Log.i("start", "" + isChecked);
        cb1.setChecked(isChecked);
        cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                Log.i("boolean", "" + isChecked);
                putBooleanInPreferences(isChecked,"isChecked");

            }
        });
    }

    public void putBooleanInPreferences(boolean isChecked, String key) {
        SharedPreferences sharedPreferences = this
                .getPreferences(Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, isChecked);
        editor.commit();
    }

    private boolean getBooleanFromPreferences(String key) {
        SharedPreferences sharedPreferences = this
                .getPreferences(Activity.MODE_PRIVATE);
        Boolean isChecked = sharedPreferences.getBoolean(key, false);
        return isChecked;
    }

    private void findID() {
        cb1 = (CheckBox) findViewById(R.id.checkBox1);
        cb2 = (CheckBox) findViewById(R.id.checkBox2);
        cb3 = (CheckBox) findViewById(R.id.checkBox3);
        cb4 = (CheckBox) findViewById(R.id.checkBox4);
    }

}

2 个答案:

答案 0 :(得分:3)

您需要更新SharedPreferencesevery时间CheckBox检查状态已更改的值。 从Settings.this.putBooleanInPreferences(isChecked,"isChecked")中的setOnCheckedChangeListener行中删除评论。

所以,你的听众如下:

cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                Log.i("boolean", "" + isChecked);

                putBooleanInPreferences(isChecked,"isChecked");
            }
        });

希望这有帮助。

答案 1 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

public class TaskList extends Activity {

    private CheckBox cb1;
    private CheckBox cb2;
    private CheckBox cb3;
    private CheckBox cb4;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_tasks);
        super.onCreate(savedInstanceState);
        cb1 = (CheckBox) findViewById(R.id.checkBox1);
        cb2 = (CheckBox) findViewById(R.id.checkBox2);
        cb3 = (CheckBox) findViewById(R.id.checkBox3);
        cb4 = (CheckBox) findViewById(R.id.checkBox4);

        sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);

        cb1.setChecked(sharedPreferences.getBoolean("cb1",false));
        cb2.setChecked(sharedPreferences.getBoolean("cb2",false));
        cb3.setChecked(sharedPreferences.getBoolean("cb3",false));
        cb4.setChecked(sharedPreferences.getBoolean("cb4",false));
        cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("cb1", isChecked);
                editor.commit();
            }
        });
        cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("cb2", isChecked);
                editor.commit();
            }
        });
        cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("cb3", isChecked);
                editor.commit();
            }
        });
        cb4.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("cb4", isChecked);
                editor.commit();
            }
        });

    }
}