Android的布尔preference问题 [英] Android boolean preference problem

查看:121
本文介绍了Android的布尔preference问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个复选框的状态保存到我的preFS。

我设置的复选框监听器,如果它被选中我做了prefs.putBoolean(cbstatus,真),这是它选中我做了prefs.putBoolean(cbstatus ,FALSE);

问题是,我在onStart()时,我得到preFS,我的布尔getcbstatus = prefs.getBoolean(cbstatus,FALSE);将始终返回一个真正的,不论如何我的听众应该pviously设置状态$ P $。

我是什么做错了吗?我有工作preFS为其他像纺纱,textviews和编辑文本,但应该是什么简单的类型(布尔)是给我一个困难时期。

我甚至试着拿出相关的听众和preF设置此复选框所有code,使整个活动只有code,与复选框交易是在该行

 布尔getcbstat = prefs.getBoolean(cbon,FALSE);
    如果(getcbstat =真){
        cb1.setChecked(真);
    }
    其他{
        cb1.setChecked(假);
        format.setVisibility(View.VISIBLE);
    }

由于没有cbon preference(我删除了所有这些),它应该返回默认和箱应该是因为选中假的。 CB1,当然是我的复选框的名称。

任何想法?

在code更新:

  OnClickListener CB =新OnClickListener(){
    公共无效的onClick(视图v){
        如果(cb1.isChecked()){
            prefs.putBoolean(cbon,真正的);
        }
        其他{
            prefs.putBoolean(cbon,FALSE);
        }
    }
};

而在调用onStart():

 布尔getcbstat = prefs.getBoolean(cbon,FALSE);
        cb1.setChecked(getcbstat);


解决方案

您已经无意中将分配给真正在你的if语句。

改成这样

 如果(getcbstat ==真)


如何使用共享preferences:

 私人共享preferences米preF;
@覆盖
公共无效的onCreate(捆绑包){
super.onCreate(包);米preF = getShared preferences(MY_ prefs_file,MODE_PRIVATE);//其他的onCreate code在这儿... ...}//你在哪里可能要保存preferences例
@覆盖
保护无效的onPause(){
super.onPause();
编辑prefEdit = pref.edit();prefEdit.putBoolean(cbon,真正的);
prefEdit.commit();}

在以后需要阅读:

 的,你可能要保存preferences //例
@覆盖
保护无效onResume(){
super.onResume();
布尔getcbstat = pref.getBoolean(cbon,FALSE);
}

这可能会是一个好主意,使preF可变一流水平,并获得preferences在OnCreate部分对象。改变MY_ prefs_file,以任何你喜欢的,但请记住,该字符串是您将用于访问该特定集合preferences从你的应用程序中的内容。我还建议使用常量,而不是原始字符串的访问键(如cbon)。

祝你好运:)

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

解决方案

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:)

这篇关于Android的布尔preference问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆