Android 微调器选择-保存到SharedReferences,然后检索
我使用我的应用程序中的“SharedReferences”来保留从多个编辑文本框保存/检索字符串值的功能,这很好。我的活动中还有一个微调器,其中包含一个字符串数组,用于显示可用值。但我不清楚如何将微调器选择写入SharedReferences,然后稍后将SharedReferences读取到retireve并设置其值 以下是我对edittext的配置: -按钮以激活将值保存到SharedReferences-Android 微调器选择-保存到SharedReferences,然后检索,android,sharedpreferences,Android,Sharedpreferences,我使用我的应用程序中的“SharedReferences”来保留从多个编辑文本框保存/检索字符串值的功能,这很好。我的活动中还有一个微调器,其中包含一个字符串数组,用于显示可用值。但我不清楚如何将微调器选择写入SharedReferences,然后稍后将SharedReferences读取到retireve并设置其值 以下是我对edittext的配置: -按钮以激活将值保存到SharedReferences- public void buttonSaveSendClick(View view)
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();
}
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);
}
-按钮以激活从SharedReferences还原上次保存的值-
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();
}
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);
}
关于如何在第一个按钮(保存)中构造微调器选定值的集合,有何建议?然后,如何在按下“还原”按钮时将保存的值返回到微调器视图?您必须调用
editor.apply()在你的编辑器.put()之后,代码>一次代码>语句。否则,您对首选项所做的所有更改都将被放弃。假设数组中的项目根本不会改变位置,那么您可以在首选项中将所选位置存储为int
要保存:
int selectedPosition = yourSpinner.getSelectedItemPosition();
editor.putInt("spinnerSelection", selectedPosition);
editor.apply();
要加载:
yourSpinner.setSelection(prefs.getInt("spinnerSelection",0));
如果数组中的项要更改,则必须存储实际字符串,而不是位置。类似这样的方法会奏效:
String selectedString = yourArray[yourSpinner.getSelectedItemPosition()];
editor.putString("spinnerSelection", selectedString);
editor.apply();
及
通过在数组中循环并对照prefs中存储的值检查数组[i],找到字符串的位置。然后调用yourSpinner.setSelected(position)
。如果改用ArrayList,则可以通过调用
ArrayList.indexOf(prefs.getString(“spinnerSelection”)代码>
请注意,只有ArrayList具有indexOf()代码>方法。在普通数组上不能使用indexOf()
method,您必须手动搜索数组以找到正确的值。我认为应该是spinner.setSelection(position)(spinner.setSelected需要布尔值