【问题标题】:How to access shared preference boolean value from another class如何从另一个类访问共享首选项布尔值
【发布时间】:2012-05-15 17:56:20
【问题描述】:

我创建了一个包含两个按钮的单选组,我可以选择一个并保存它,它工作正常,关闭应用程序后仍然存储。我想要做的是使用另一个类中单选按钮的值。这是我的设置类,其中包含共享首选项代码:

public class Settings extends Activity {
private String settingsTAG = "AppNameSettings";
private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.settings);
    prefs = getSharedPreferences(settingsTAG, 0);

    final RadioButton rb0 = (RadioButton) findViewById(R.id.radioInternetYes);
    final RadioButton rb1 = (RadioButton) findViewById(R.id.radioInternetNo);

    rb0.setChecked(prefs.getBoolean("rb0", true));
    rb1.setChecked(prefs.getBoolean("rb1", false));     
    Button btnSave = (Button) findViewById(R.id.btnSave);

    btnSave.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            prefs = getSharedPreferences(settingsTAG, 0);
            Editor editor = prefs.edit();

            editor.putBoolean("rb0", rb0.isChecked());
            editor.putBoolean("rb1", rb1.isChecked());
            editor.commit();

            finish();

        }
    } );

}

在另一个类中,我试图在单击按钮时检查 rb0 是真还是假:

share.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            if(rb0 = true){
            Intent i = new Intent(Intent.ACTION_SEND);

            i.setType("text/plain");

            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{""});

            i.putExtra(Intent.EXTRA_SUBJECT, "Check out my awesome score!");
            i.putExtra(Intent.EXTRA_TEXT   , "I am playing this awesome game called Tap the Button, and I just scored the incredible highscore of " +s+ " Taps!!\n\nCan you beat it!?");
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(FailScreen.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
        }
            else{
                //Display warning that rb0 is false
            }
                }
    });

我研究了 Stackoverflow 和开发者文档,但我似乎无法找到如何做到这一点,任何建议都值得赞赏。

谢谢

【问题讨论】:

    标签: android sharedpreferences


    【解决方案1】:

    您需要再次访问 SharedPreferences 而不是 if(rb0 = true)(无论如何应该是 ==)。引用自http://developer.android.com/guide/topics/data/data-storage.html

    要读取值,请使用 SharedPreferences 方法,例如 getBoolean() 和 获取字符串()。

    所以你会使用类似的东西:

    String settingsTAG = "AppNameSettings";
    SharedPreferences prefs = getSharedPreferences(settingsTAG, 0);
    boolean rb0 = prefs.getBoolean("rb0", false);
    if(rb0 == true){ 
        // Do something
    }
    

    【讨论】:

    • 感谢您的回答!我不知道的一个问题是如何在这个类中使用设置,我是否需要创建一个实例以便我可以从另一个类访问它?使用你的代码我得到一个错误,因为它不知道设置是什么,我只是不知道如何从这个类访问设置。谢谢!
    • 抱歉,我忘记在示例代码中声明 SharedPreferences。是的,您需要在此类中创建它的实例。我编辑了响应以创建一个类似于您发布的代码的实例。
    • 这很完美!谢谢你! :) 我正在尝试各种疯狂的东西,我需要一段时间才能弄清楚那些代码行!你帮我省了很多麻烦谢谢!! :)
    【解决方案2】:

    你也可以使用:

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    sharedPreferences.getBoolean("myKey", false)
    

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 2017-10-28
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多