我是 Android 新手,我不知道如何检索使用 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”,但我不知道如何