Android - >在主活动中加载共享首选项

时间:2012-09-27 00:37:45

标签: android sharedpreferences

我创建了共享首选项活动,并且我保存了2个字符串...

    public class sharedprefs extends Activity {


    EditText email;
    EditText lozinka;
    Button btnEmail;
    Button btnLozinka;
    Button btnPovratak;
    TextView email2; 
    TextView lozinka2;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sharedpref);

        email2 = (TextView)findViewById(R.id.textView4);
        lozinka2 = (TextView)findViewById(R.id.textView5);

        email = (EditText)findViewById(R.id.editText1);
        lozinka = (EditText)findViewById(R.id.editText2);

        btnEmail = (Button) findViewById(R.id.button1);
        btnLozinka = (Button) findViewById(R.id.button2);
        btnPovratak = (Button) findViewById(R.id.button3);

        LoadPreferences();

        btnEmail.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {   

                SavePreferences("EMAIL", email.getText().toString());
                LoadPreferences();  


            }
        });

        btnLozinka.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {   

                SavePreferences("LOZINKA", lozinka.getText().toString());
                LoadPreferences();


            }
        });

        btnPovratak.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {   

                finish();


            }
        });
    }

    private void SavePreferences(String key, String value){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
       }

       private void LoadPreferences(){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String stremail = sharedPreferences.getString("EMAIL", "");
        String strlozinka = sharedPreferences.getString("LOZINKA", "");
        email2.setText(stremail);
        lozinka2.setText(strlozinka);
       }


}

我测试了它并用textview显示...当我退出并重新进入我的应用程序时,它们仍然显示。现在我需要这两个字符串用于我的主要活动中的httpClient。问题是,我不知道如何在我的主要活动中加载它们,我需要做些什么(在主要活动中声明)才能使它工作?

2 个答案:

答案 0 :(得分:2)

查看instructions on the Android Developers documentation以处理多个活动中的SharedPreferences。这也是been covered in another answer

访问SharedPreferences的推荐方法是:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

所以在你的情况下,你可以把它写成:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

这将在PreferenceActivity和普通的主Activity中发挥作用。

答案 1 :(得分:1)

您需要调用相同的方法LoadPreferences(),您需要在主要活动(或您希望此SharedPreference的任何其他活动)中进行编码。

private void LoadPreferences()
{
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String stremail = sharedPreferences.getString("EMAIL", "");
        String strlozinka = sharedPreferences.getString("LOZINKA", "");
        // Strings variable are ready with the values, you can assign them to other component if you want
}