存储多个复选框状态(启用/禁用)

Store multiple checkbox states (enabled / disabled)

当我 select 某些复选框将启用其他复选框时,我正在制作一个应用程序,我想保留这些状态。我正在尝试使用 sharedPreferences,但它没有给我结果,我不知道为什么。有人可以帮我处理代码吗?

保存状态:

private void saveContadors() {
    saved=true;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("c0", contador0.isEnabled());
    editor.putBoolean("c1", contador1.isEnabled());
    editor.putBoolean("c2", contador2.isEnabled());
    editor.putBoolean("c3", contador3.isEnabled());
    editor.putBoolean("c4", contador4.isEnabled());
    editor.putBoolean("c5", contador5.isEnabled());
    editor.putBoolean("c6", contador6.isEnabled());
    editor.putBoolean("c7", contador7.isEnabled());
    editor.putBoolean("c8", contador8.isEnabled());
    editor.putBoolean("c9", contador9.isEnabled());
    editor.putBoolean("c10", contador10.isEnabled());
    editor.putBoolean("c11", contador11.isEnabled());
    editor.putBoolean("c12", contador12.isEnabled());
    editor.commit();
}

加载状态:

private void loadContadors() {
    Toast.makeText(this, "load"+contador6.isEnabled(), Toast.LENGTH_SHORT).show();

    if(saved==true){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        /*for (int i = 0; i < contador.length; i++)
            contador[i].setEnabled(prefs.getBoolean("contador" + i, true));*/
        contador0.setEnabled(prefs.getBoolean("c0", false));
        contador1.setEnabled(prefs.getBoolean("c1", false));
        contador2.setEnabled(prefs.getBoolean("c2", false));
        contador3.setEnabled(prefs.getBoolean("c3", false));
        contador4.setEnabled(prefs.getBoolean("c4", false));
        contador5.setEnabled(prefs.getBoolean("c5", false));
        contador6.setEnabled(prefs.getBoolean("c6", false));
        contador7.setEnabled(prefs.getBoolean("c7", false));
        contador8.setEnabled(prefs.getBoolean("c8", false));
        contador9.setEnabled(prefs.getBoolean("c9", false));
        contador10.setEnabled(prefs.getBoolean("c10", false));
        contador11.setEnabled(prefs.getBoolean("c11", false));
        contador12.setEnabled(prefs.getBoolean("c12", false));



          for(i=0;i<contador.length;i++){
                if(contador[i].isEnabled()){
                    contador[i].setBackgroundResource(R.drawable.amarillo);
                }

                if(contador[i].isEnabled() && contador[i].isChecked()){
                    contador[i].setBackgroundResource(R.drawable.naranja);          
                }   

                if(!contador[i].isEnabled()){
                    contador[i].setBackgroundResource(R.drawable.gris); 
                }

                if(contador[i].isEnabled() && !contador[i].isChecked()){
                    contador[i].setBackgroundResource(R.drawable.amarillo);
                    }
                }

    }
}

并调用这些方法:

    @Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    saveContadors();
    Toast.makeText(this, "save", Toast.LENGTH_SHORT).show();

}

@Override
protected void onResume() {
    super.onPostResume();
    loadContadors();

}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    saveContadors();
    Toast.makeText(this, "save"+contador6.isEnabled(), Toast.LENGTH_SHORT).show();

}

如果启用或禁用,我想保留。我不想保存 if selected.

查看 CheckBoxPreference:http://developer.android.com/reference/android/preference/CheckBoxPreference.html

它已内置到默认的 SharedPreferences 中,因此更新偏好设置变得轻而易举。

我的代码没问题。错误是没有保存布尔值 "saved"!。一个愚蠢的错误。代码如下所示:

private void saveContadors() {
        saved=true;

...

private void loadContadors() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        saved=prefs.getBoolean("saved", false);