如何从另一个类访问共享preference布尔值布尔值、preference

2023-09-06 16:25:12 作者:金钱打造实力狗!

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

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

        }
    } );

}

在其它类我想检查是否RB0是真的还是假单击按钮时:

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

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

我已经研究和#2的开发者文档,但我似乎无法找出如何可以做到这一点,任何的建议是AP preciated。

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

感谢您

推荐答案

而不是如果(RB0 = TRUE)(这应该是 == 反正),你需要再次访问共享preferences。从 http://developer.android.com/guide/topics/data报价/data-storage.html :

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:

要读取数值,使用共享preferences方法,如getBoolean()和  的getString()。

To read values, use SharedPreferences methods such as getBoolean() and 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
}
 
精彩推荐
图片推荐