Android 如何从另一个类访问共享首选项布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10606237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:19:58  来源:igfitidea点击:

How to access shared preference boolean value from another class

androidsharedpreferences

提问by deucalion0

I have created a radio group which contains two buttons, I can select one and save it, it works fine, still stored after closing the app. What I want to do is use the value of the radio buttons in another class. Here is my settings class which contains the shared preferences code:

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

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

        }
    } );

}

In another class I am trying to check if rb0 is true or false when clicking a button:

在另一堂课中,我试图在单击按钮时检查 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
            }
                }
    });

I have researched Stackoverflow and the developer documentation, but I cant seem to find out how this can be done, any advice is appreciated.

我已经研究了 Stackoverflow 和开发人员文档,但我似乎无法找到如何做到这一点,任何建议表示赞赏。

Thank you

谢谢

回答by Jon

Instead of if(rb0 = true)(which should be ==anyways), you need to access the SharedPreferences again. Quoted from http://developer.android.com/guide/topics/data/data-storage.html:

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

To read values, use SharedPreferences methods such as getBoolean() and getString().

要读取值,请使用 SharedPreferences 方法,例如 getBoolean() 和 getString()。

So you would use something like:

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

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

回答by CommonSenseCode

You can also use:

您还可以使用:

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