Android 布尔偏好问题
我希望将复选框的状态保存到我的首选项中。
我在复选框上设置了一个侦听器,如果选中它,我会执行 prefs.putBoolean("cbstatus", true),如果未选中,我会执行 prefs.putBoolean("cbstatus", false);
问题是,在我的 onStart() 中,当我获取首选项时,我的 Boolean getcbstatus = prefs.getBoolean("cbstatus", false);将始终返回 true,无论我的侦听器之前应如何设置该状态。
我做错了什么?我有其他东西的工作首选项,如旋转器、文本视图和编辑文本,但最简单的类型(布尔值)却给我带来了困难。
我什至尝试取出与此复选框的侦听器和首选项设置相关的所有代码,以便整个活动中处理该复选框的唯一代码位于该行中,
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() 中:
Boolean getcbstat = prefs.getBoolean("cbon", false);
cb1.setChecked(getcbstat);
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);
Trouble is, in my onStart() when I get prefs, my Boolean getcbstatus = prefs.getBoolean("cbstatus", false); will always return a true, regardless of how my listener should have set that status previously.
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. cb1, of course, is the name of my checkbox.
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():
Boolean getcbstat = prefs.getBoolean("cbon", false);
cb1.setChecked(getcbstat);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不小心在 if 语句中将其指定为 true。
将其更改为
[编辑--如何使用共享首选项(而不是Java的首选项类)]
如何使用 SharedPreferences:
当您稍后需要阅读它时:
将 pref 变量设置为类级别并在 onCreate 部分获取首选项对象可能是一个好主意。将“my_prefs_file”更改为您喜欢的任何内容,但请记住该字符串是您将用来从应用程序内访问该特定首选项集的字符串。我还建议使用常量而不是原始字符串作为访问键(例如“cbon”)。
祝你好运:)
You've accidentally assigned it to true in your if statement.
Change it to this
[Edit -- How to use shared preferences (instead of Java's preferences class)]
How to use SharedPreferences:
When you need to read it later:
It would probably be a good idea to make the pref variable class level and get the preferences object in the onCreate section. 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. I also recommend using constants instead of raw strings for the access keys (like "cbon").
Good luck:)