2014-11-21 47 views
0

我有一个Android应用程序,在用户选中某个单选按钮的活动之一中。我想保存用户的选择并在其他活动中使用它的值。如何在Android中使用sharedPreferences?

+1

这是非常基本的,并在[文档](HTTPS解释://显影剂.android.com/guide/topics/data/data-storage.html #pref) – 2014-11-21 16:44:55

+0

[如何在Android中使用SharedPreferences来存储,获取和编辑值](https://stackoverflow.com/questions/3624280 /如何使用的,sharedpreferences功能于Android的到店,读取和编辑-VAL的UE) – 2017-09-22 16:08:37

回答

1

要存储

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(); 
prefs.edit().putBoolean("KEY", your_boolean).commit(); 

要检索

Boolean your_boolean = prefs.getBoolean("KEY", false); 
1

您可以使用共享偏好保存的值。

保存值到SharedPreferences使用以下代码

SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(
         "SHARED_PREF", MODE_PRIVATE).edit(); 
       editor.putString("radio_value", value); 
       editor.commit(); 

对于从SharedPreferences Retriving值使用以下代码

SharedPreferences prefs = getApplicationContext().getSharedPreferences(
      "SHARED_PREF", MODE_PRIVATE); 
    String storedValue = prefs.getString("radio_value",""); 
相关问题