我在我的应用程序中使用"SharedPreferences"来保留从多个edittext框保存/检索字符串值的功能,这样就可以了.我的活动中还有一个Spinner,它有一个字符串数组,用于它的可用值.但我不清楚如何将微调器选择写入SharedPreferences,然后读取SharedPreferences以退出并设置它的值.
这是我对edittext的配置:
-Button激活保存值到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();
}
Run Code Online (Sandbox Code Playgroud)
-Button激活从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);
}
Run Code Online (Sandbox Code Playgroud)
有关如何在第一个按钮(保存)中构建微调器选定值的集合的任何建议?那么如何在按下"恢复"按钮时将保存的值返回到微调器视图?