【问题标题】:if application re-open SharedPreferences not saving boolean如果应用程序重新打开 SharedPreferences 不保存布尔值
【发布时间】:2015-08-12 16:18:23
【问题描述】:

我的项目中有以下代码,名为menu.class

f1 =(Button)findViewById(R.id.f1);      
f1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){
        // TODO Auto-generated method stub
        Intent intent = new Intent ();
        intent.setClassName ("com.example.aplication", "com.example.application.levelone");
        startActivityForResult (intent, 0);              
    }             
}); 
}

public void onActivityResult (int requestCode, int resultCode, Intent intent) {
    super.onActivityResult (requestCode, resultCode, intent);

    f2=(Button)findViewById(R.id.f2);      
    f2lock=(ImageView)findViewById(R.id.f2lock);

    switch (resultCode) {
        case 11: f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);               
    }                                  
    SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
    boolean levelTwoUnlocked = preferences.getBoolean("f2", true);

    if(levelTwoUnlocked){
        f2.setVisibility(View.VISIBLE);
        f2lock.setVisibility(View.GONE);
    }
    else {
        f2.setVisibility(View.GONE);
        f2lock.setVisibility(View.VISIBLE);
    } 

    f2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            Intent intent = new Intent ();
            intent.setClassName ("com.example.application", "com.example.application.leveltwo");
            startActivityForResult (intent, 0);              
        }             
    });     
}

代码运行良好。 f2 按钮设置为可见,f2lock 不可见。 但是当我重新打开应用程序时,f2 按钮又恢复了可见。 我的偏好设置代码没有完成吗?

更新

我已经像这样更改了偏好代码

    SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
    SharedPreferences.Editor ed = preferences.edit();
    boolean levelTwoUnlocked = preferences.getBoolean("f2", true);
    ed.commit();                

当我重新打开应用程序时,它仍然遇到同样的问题

【问题讨论】:

  • 您是否将布尔值添加到您的共享首选项中,如果是,则发布代码 sn-p。
  • 正如我所想,我们没有看到提交操作。
  • 是的,您在哪里编辑 SharedPreferences 共享该代码。
  • @KristyWelsh 你能告诉我如何提交();在我上面的代码中操作?

标签: android boolean sharedpreferences preferences


【解决方案1】:

您正在检索f2 的值,但您似乎没有将其保存在任何地方。

preferences.getBoolean("f2", true);

如果 f2 不存在(即您从未将其保存在 SharedPreference 实例中),这将返回最后保存的 f2true 值 - 它不会创建或保存任何值为您提供 SharedPreferences 实例。

要保存值,您需要创建一个 SharedPreferences 编辑器,设置要保存的值(或多个值)并提交:

SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("f2", levelTwoUnlocked);
editor.commit();

现在,当您下次读取 f2 值时,它将具有您上次提交的任何值。

【讨论】:

  • @Chairizky 再看看我的答案 - 你在你的 preferences 对象上调用 getBoolean。您应该在您的 editor 实例上调用 putBoolean
  • 所以 false 是更改为 levelTwoUnlocked 吗?它显示像这样的错误兄弟:levelTwoUnlocked 无法解析为变量
  • 您不必使用levelTwoUnlocked - 我只是以它为例。您可以将值设置为 true、false 或使用您喜欢的任何布尔变量。
猜你喜欢
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
相关资源
最近更新 更多