Android 布尔偏好问题

发布于 2024-09-26 02:50:16 字数 1159 浏览 0 评论 0原文

我希望将复选框的状态保存到我的首选项中。

我在复选框上设置了一个侦听器,如果选中它,我会执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪若未夕 2024-10-03 02:50:16

您不小心在 if 语句中将其指定为 true。

将其更改为

if (getcbstat == true)

[编辑--如何使用共享首选项(而不是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();

}

当您稍后需要阅读它时:

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

将 pref 变量设置为类级别并在 onCreate 部分获取首选项对象可能是一个好主意。将“my_prefs_file”更改为您喜欢的任何内容,但请记住该字符串是您将用来从应用程序内访问该特定首选项集的字符串。我还建议使用常量而不是原始字符串作为访问键(例如“cbon”)。

祝你好运:)

You've accidentally assigned it to true in your if statement.

Change it to this

if (getcbstat == true)

[Edit -- How to use shared preferences (instead of Java's preferences class)]
How to use 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. 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:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文