使用共享首选项在单击按钮时保存字符串
Using Shared preferences to save a string on a button click
所以目前我有:
public void SaveText(View view) {
String saved = text.getText().toString();
// TODO
Toast.makeText(getApplicationContext(), R.string.addedfavs, Toast.LENGTH_SHORT).show();
}
如何保存 'Text' 字符串并将其保存到共享首选项中,而不覆盖以前保存的字符串。
(注意:我试过使用数组和数组列表,但我只是得到一个结果并覆盖它而不是添加到以前保存的字符串中)
我对此很陌生,所以请不要跳过任何步骤,无论它们看起来多么微不足道。
读取已存储在您的 SharedPreferences 中的内容,附加新值,最后写回 SharedPreferences。
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
同时保存
public void SaveText(View view) {
String saved = text.getText().toString();
SharedPreferences.Editor editor=prefs.edit();
editor.put("value",saved);
editor.commit();
Toast.makeText(getApplicationContext(), R.string.addedfavs, Toast.LENGTH_SHORT).show();
}
检索时
String value=prefs.getString("value");
所以目前我有:
public void SaveText(View view) {
String saved = text.getText().toString();
// TODO
Toast.makeText(getApplicationContext(), R.string.addedfavs, Toast.LENGTH_SHORT).show();
}
如何保存 'Text' 字符串并将其保存到共享首选项中,而不覆盖以前保存的字符串。
(注意:我试过使用数组和数组列表,但我只是得到一个结果并覆盖它而不是添加到以前保存的字符串中)
我对此很陌生,所以请不要跳过任何步骤,无论它们看起来多么微不足道。
读取已存储在您的 SharedPreferences 中的内容,附加新值,最后写回 SharedPreferences。
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
同时保存
public void SaveText(View view) {
String saved = text.getText().toString();
SharedPreferences.Editor editor=prefs.edit();
editor.put("value",saved);
editor.commit();
Toast.makeText(getApplicationContext(), R.string.addedfavs, Toast.LENGTH_SHORT).show();
}
检索时
String value=prefs.getString("value");