我在我的应用程序中使用“SharedPreferences”来保留从多个编辑文本框中保存/检索字符串值的能力,这工作得很好。我的活动中还有一个 Spinner,它带有一个字符串数组,它的可用值。但我不清楚如何将微调器选择写入 SharedPreferences,然后再阅读 SharedPreferences 以退休并设置它的值。
这是我对edittext的配置:
-激活将值保存到 SharedPreferences 的按钮-
public void buttonSaveSendClick(View view) {
SharedPreferences.Editor editor = getPreferences(0).edit();
EditText editTextCallId = (EditText) findViewById(R.id.editTextCallId);
editor.putString("editTextCallIdtext", editTextCallId.getText().toString());
editor.putInt("selection-startCallId", editTextCallId.getSelectionStart());
editor.putInt("selection-endCallId", editTextCallId.getSelectionEnd());
editor.commit();
}
-激活从 SharedPreferences 恢复上次保存的值的按钮-
public void buttonRestoreLastClick(View view) {
SharedPreferences prefs = getPreferences(0);
EditText editTextCallId = (EditText) findViewById(R.id.editTextCallId);
String editTextCallIdtextrestored = prefs.getString("editTextCallIdtext", null);
editTextCallId.setText(editTextCallIdtextrestored, EditText.BufferType.EDITABLE);
int selectionStartCallId = prefs.getInt("selection-startCallId", -1);
int selectionEndCallId = prefs.getInt("selection-endCallId", -1);
editTextCallId.setSelection(selectionStartCallId, selectionEndCallId);
}
关于如何在第一个按钮(保存)中构造微调器选定值的集合的任何建议?那么如何在按下“恢复”按钮时将该保存的值返回到微调器视图?