-
撒科打诨
要在共享首选项中存储值:SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","Harneet");editor.apply();要从共享首选项中检索值:SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);String name = preferences.getString("Name", "");
if(!name.equalsIgnoreCase("")){
name = name + " Sethi"; /* Edit the value here*/}
-
呼如林
要编辑从数据sharedpreference SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("text", mSaved.getText().toString());
editor.putInt("selection-start", mSaved.getSelectionStart());
editor.putInt("selection-end", mSaved.getSelectionEnd());
editor.apply();为了获取从数据sharedpreferenceSharedPreferences prefs = getPreferences(MODE_PRIVATE); String restoredText = prefs.getString("text", null);if (restoredText != null) {
//mSaved.setText(restoredText, TextView.BufferType.EDITABLE);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);
/*if (selectionStart != -1 && selectionEnd != -1)
{
mSaved.setSelection(selectionStart, selectionEnd);
}*/}编辑我从API Demo示例中获取了这个片段。那里有一个EditText盒子。在这context不是必需的。我正在评论相同的。
-
MMMHUHU
来写 :SharedPreferences preferences = getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_WORLD_WRITEABLE);SharedPreferences.Editor editor = preferences.edit();editor.putString("Authentication_Id",userid.getText().toString());editor.putString("Authentication_Password",password.getText().toString());editor.putString("Authentication_Status","true");editor.apply();阅读 :SharedPreferences prfs = getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_PRIVATE);String Astatus = prfs.getString("Authentication_Status", "");
-
料青山看我应如是
在偏好中设置值:// MY_PREFS_NAME - a static String variable like: //public static final String MY_PREFS_NAME = "MyPrefsFile";SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString("name", "Elena"); editor.putInt("idName", 12); editor.commit();从首选项中检索数据:SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String restoredText = prefs.getString("text", null);if (restoredText != null) { String name = prefs.getString("name", "No name defined");//"No name defined" is the default value. int idName = prefs.getInt("idName", 0); //0 is the default value.}更多信息:使用共享首选项共享首选项