【问题标题】:Sharedpreferences doesnt work, checkbox should stay in the same state when I am closing/opening the appSharedpreferences 不起作用,当我关闭/打开应用程序时,复选框应保持相同状态
【发布时间】:2014-09-01 18:43:14
【问题描述】:

嘿伙计们,我想让我的复选框在每次打开我的应用程序时都保持在相同的状态。我用 'ja/nein' 字符串得到这个,当我关闭并再次打开我的应用程序时,字符串状态...但我的 checkbox.setchecked(true/false) 不起作用..请帮助

public void changeVisitStatus(){
    SharedPreferences visitStatus = mData.getVisitStatus();
    SharedPreferences.Editor editor = visitStatus.edit();

    if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
        editor.putString(mData.mVisitKey, "ja");
        editor.commit();
        mGUI.mBtnVisit.setChecked(true);
    }
    else{
        editor.putString(mData.mVisitKey, "nein");
        editor.commit();
        mGUI.mBtnVisit.setChecked(false);
    }
    mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}

编辑:我尝试了另一种方法..我认为它会更好,但效果不佳..

public void changeVisitStatus(){
    SharedPreferences visitStatus = mData.getVisitStatus();
    SharedPreferences.Editor editor = visitStatus.edit();

    if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
        editor.putString(mData.mVisitKey, "ja");
        editor.putBoolean("isChecked", true);
        editor.commit();

    }
    else{
        editor.putString(mData.mVisitKey, "nein");
        editor.putBoolean("isChecked", false);
        editor.commit();

    }
    mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}

并将这个放入我的活动中的 onCreate(Bundle savedInstanceState)

mGUI.mBtnVisit.setChecked(mData.getVisitStatus().getBoolean("isChecked", false));

【问题讨论】:

    标签: android checkbox sharedpreferences ischecked


    【解决方案1】:

    试试这样:

       public void putBooleanInPreferences(boolean isChecked,String key){
            SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean(key, isChecked);
            editor.commit();        
        }
        public boolean getBooleanFromPreferences(String key){
            SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
            Boolean isChecked = sharedPreferences.getBoolean(key, false);
            return isChecked;       
        }
    

    并在 onCreate()

     CheckBox checkBox = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            checkBox = (CheckBox) findViewById(R.id.my_check_box);
            boolean isChecked = getBooleanFromPreferences("isChecked");
            Log.i("start",""+isChecked);
            checkBox.setChecked(isChecked);
            checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                    Log.i("boolean",""+isChecked);
                    TestActivity.this.putBooleanInPreferences(isChecked,"isChecked");
                }
            });
        }
    

    希望对您有所帮助!

    【讨论】:

    • 默认值应该是false
    • @Krupa Patel 谢谢!我试过你的代码,但仍然不能像我想象的那样工作......选中 - 'ja' ...未选中 - 'No' ... :((
    【解决方案2】:

    您只向我们展示了更改状态的代码,可能是从复选框的 OnClick 侦听器调用的。

    您还应该添加仅从 SharedPreferences 读取状态并据此设置复选框状态的代码(可以是相同的代码,但 if 条件被否定)。

    您需要从 OnCreate 事件中调用该代码。

    public void setVisitStatus(){
        SharedPreferences visitStatus = mData.getVisitStatus();
        mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
    }
    

    【讨论】:

    • 不,我的复选框没有 OnClick 监听器。我以为我可以处理有关 changig 状态的任何事情(如果该状态 - 那么复选框也会状态?)我不明白您所说的仅读取状态并设置复选框状态是什么意思? ...您是指第一次尝试还是编辑? @velis
    • 我如何只从 SharedPreferences 中读取状态.. 对不起我的问题.. 我只是一个初学者并尝试学习
    • 我已经在我的回答中为您编写了代码。将这两行放在您的 OnCreate 方法中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2013-02-18
    • 2017-11-22
    • 2021-07-17
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多