我是新來的Android,我不知道如何以檢索使用sharedpreferences存儲的值值,我找不到什麼,我必須對其他活動寫來使用這些值的任何實例。如果有人能幫助我,我真的很感激它檢索使用Sharedpreferences
這是怎麼了存儲數據:
protected void onResume() {
super.onResume();
SharedPreferences prefs = getPreferences(0);
String restoredText1 = prefs.getString("cpw", "30");
if (restoredText1 != null) {
savedcostperworker.setText(restoredText1, TextView.BufferType.EDITABLE);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);
if (selectionStart != -1 && selectionEnd != -1) {
savedcostperworker.setSelection(selectionStart, selectionEnd);
}
}
}
protected void onPause() {
super.onPause();
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putString("cpw", savedcostperworker.getText().toString());
editor.putInt("selection-start", savedcostperworker.getSelectionStart());
editor.putInt("selection-end", savedcostperworker.getSelectionEnd());
}
private EditText savedcostperworker;
現在我要顯示在另一個活動一個TextView值「CPW」,但我不「知道你怎麼
你缺少editor.commit(),這是必須的需要後,將值editor.putInt()..,否則修改將不會被放到首選項file.To得到其他活動這個價值,你需要使用getSharedPreferences ().. – ridoy