5 回答
TA贡献1934条经验 获得超2个赞
要在共享首选项中存储值:
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*/}
TA贡献1798条经验 获得超3个赞
要编辑从数据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();
为了获取从数据sharedpreference
SharedPreferences 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
不是必需的。我正在评论相同的。
TA贡献1834条经验 获得超8个赞
来写 :
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", "");
TA贡献1772条经验 获得超8个赞
在偏好中设置值:
// 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.
}
更多信息:
添加回答
举报