Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android布尔偏好问题_Java_Android_Boolean_Preferences - Fatal编程技术网

Java Android布尔偏好问题

Java Android布尔偏好问题,java,android,boolean,preferences,Java,Android,Boolean,Preferences,我想将复选框的状态保存到我的首选项中 我在复选框上设置了一个侦听器,如果选中它,我将执行prefs.putBoolean(“cbstatus”,true),如果不选中它,我将执行prefs.putBoolean(“cbstatus”,false) 问题是,在onStart()中,当我获取prefs时,我的布尔值getcbstatus=prefs.getBoolean(“cbstatus”,false);将始终返回true,而不管我的侦听器以前应该如何设置该状态 我做错了什么?我对其他东西有工作优

我想将复选框的状态保存到我的首选项中

我在复选框上设置了一个侦听器,如果选中它,我将执行prefs.putBoolean(“cbstatus”,true),如果不选中它,我将执行prefs.putBoolean(“cbstatus”,false)

问题是,在onStart()中,当我获取prefs时,我的布尔值getcbstatus=prefs.getBoolean(“cbstatus”,false);将始终返回true,而不管我的侦听器以前应该如何设置该状态

我做错了什么?我对其他东西有工作优先权,比如微调器、文本视图和编辑文本,但最简单的类型(布尔型)给我带来了困难

我甚至尝试删除与此复选框的侦听器和pref设置相关的所有代码,以便整个活动中处理该复选框的唯一代码都在该行中

Boolean getcbstat = prefs.getBoolean("cbon", false);
    if (getcbstat = true) {
        cb1.setChecked(true);
    }
    else {
        cb1.setChecked(false);
        format.setVisibility(View.VISIBLE);
    }
因为没有cbon首选项(我将它们全部删除),所以默认情况下它应该返回false,并且应该取消选中该框。当然,cb1是我复选框的名称

有什么想法吗

守则更新:

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


您在if语句中意外地将其指定为true

换成这个

if (getcbstat == true)
[编辑--如何使用共享首选项(而不是Java的首选项类)] 如何使用SharedReferences:

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();

}
当您以后需要阅读时:

//Example of where you might want to save preferences
@Override
protected void onResume() {
super.onResume();
boolean getcbstat = pref.getBoolean("cbon", false);
}
最好将pref变量类设置为level,并在onCreate部分中获取preferences对象。将“my_prefs_file”更改为您喜欢的任何内容,但请记住,您将使用该字符串从应用程序中访问特定的首选项集。我还建议对访问键使用常量而不是原始字符串(如“cbon”)


祝你好运:)

好吧,我这么做了,但现在它总是返回false。我只是不明白现在是什么机修工在工作。。。如果代码的唯一问题是“==”,为什么它总是返回false?我甚至设置了一个离散按钮,将cbon pref设置为true,与侦听器无关。它仍然不起作用,并且总是出现错误。请显示设置首选项的代码。另外,确保正在运行commit();在您的首选项中,在完成赋值后编辑对象。您能解释一下commit()是什么吗;做我没有使用SharedReferences,我也不确定什么是首选项编辑对象。如果你没有使用SharedReferences,那么你上面使用的pref对象是什么?你能给我们看一下声明吗?Preferences prefs=Preferences.userNodeForPackage(getClass());
//Example of where you might want to save preferences
@Override
protected void onResume() {
super.onResume();
boolean getcbstat = pref.getBoolean("cbon", false);
}