简体   繁体   English

Android布尔偏好设置问题

[英]Android boolean preference problem

I want to have the status of a checkbox be saved into my prefs. 我想将复选框的状态保存到我的偏好中。

I set a listener on the checkbox, and if it is checked I do a prefs.putBoolean("cbstatus", true), and it is it unchecked i do a prefs.putBoolean("cbstatus", false); 我在复选框上设置了一个侦听器,如果选中,则执行prefs.putBoolean(“ cbstatus”,true);如果未选中,则执行prefs.putBoolean(“ cbstatus”,false);。

Trouble is, in my onStart() when I get prefs, my Boolean getcbstatus = prefs.getBoolean("cbstatus", false); 麻烦的是,当我获得偏好时,在我的onStart()中,我的布尔值getcbstatus = prefs.getBoolean(“ cbstatus”,false); will always return a true, regardless of how my listener should have set that status previously. 不管我的听众以前如何设置该状态,都将始终返回true。

What am I doing wrong? 我究竟做错了什么? I have working prefs for other things like spinners, textviews, and edit texts, but what should be the simplest type (a boolean) is giving me a hard time. 我在处理微调器,文本视图和编辑文本等其他方面都有工作偏好,但是最简单的类型(布尔值)应该给我带来麻烦。

I've even tried taking out all code related to listeners and pref setting for this checkbox, so that the only code in the entire activity that deals with the checkbox is in the line 我什至尝试删除与此复选框相关的所有与侦听器和首选项设置相关的代码,因此在处理该复选框的整个活动中唯一的代码在该行中

Boolean getcbstat = prefs.getBoolean("cbon", false);
    if (getcbstat = true) {
        cb1.setChecked(true);
    }
    else {
        cb1.setChecked(false);
        format.setVisibility(View.VISIBLE);
    }

Since there is no cbon preference (i deleted them all), it should return false by default and the box should be unchecked since. 由于没有碳纤维偏好(我已将它们全部删除),因此默认情况下它应该返回false,并且此后应取消选中该框。 cb1, of course, is the name of my checkbox. cb1当然是我的复选框的名称。

Any ideas? 有任何想法吗?

Update on the code: 更新代码:

OnClickListener cb = new OnClickListener() {
    public void onClick(View v) {
        if (cb1.isChecked()) {
            prefs.putBoolean("cbon", true);
        }
        else {
            prefs.putBoolean("cbon", false);
        }
    }
};

And in the onStart(): 在onStart()中:

        Boolean getcbstat = prefs.getBoolean("cbon", false);
        cb1.setChecked(getcbstat);

You've accidentally assigned it to true in your if statement. 您在if语句中不小心将其分配为true。

Change it to this 把它改成这个

if (getcbstat == true)

[Edit -- How to use shared preferences (instead of Java's preferences class)] How to use SharedPreferences: [编辑-如何使用共享首选项(而不是Java的首选项类)]如何使用SharedPreferences:

private SharedPreferences mPref;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);

mPref = getSharedPreferences("my_prefs_file", MODE_PRIVATE);

//Other onCreate code goes here...

}  

//Example of where you might want to save preferences
@Override
protected void onPause() {
super.onPause();
Editor prefEdit = pref.edit();

prefEdit.putBoolean("cbon", true);
prefEdit.commit();

}

When you need to read it later: 当您以后需要阅读时:

//Example of where you might want to save preferences
@Override
protected void onResume() {
super.onResume();
boolean getcbstat = pref.getBoolean("cbon", false);
}

It would probably be a good idea to make the pref variable class level and get the preferences object in the onCreate section. 使首选变量类级别并在onCreate节中获取首选项对象可能是一个好主意。 Change "my_prefs_file" to whatever you like, but remember that that string is what you will use to access that that particular set of preferences from within your application. 将“ my_prefs_file”更改为所需的名称,但请记住,该字符串是用于从应用程序中访问特定的一组首选项的字符串。 I also recommend using constants instead of raw strings for the access keys (like "cbon"). 我还建议为访问密钥使用常量而不是原始字符串(例如“ cbon”)。

Good luck:) 祝好运:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM