我有一些设置值的微调器,其中四个。我希望他们在启动之间保留当前设置。当您在微调器中选择一个值时,它不应保存该值,直到按下 updateSettings 按钮。我该怎么做?
这是一些代码,在 onCreate 我使用默认值制作微调器:
mBaudSpinner = (Spinner) findViewById(R.id.bannerBaudSpinner);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mBaudSpinner.setAdapter(adapter);
String[] tempArray = SlickUSB2Serial.BAUD_RATES;
for (int i = 0; i < tempArray.length; i++) {
adapter.add(tempArray[i]);
}
mBaudSpinner.setSelection(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal());
mDataSpinner = (Spinner) findViewById(R.id.bannerDataSpinner);
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mDataSpinner.setAdapter(adapter);
tempArray = SlickUSB2Serial.DATA_BITS;
for (int i = 0; i < tempArray.length; i++) {
adapter.add(tempArray[i]);
}
mDataSpinner
.setSelection(SlickUSB2Serial.DataBits.DATA_8_BIT.ordinal());
mParitySpinner = (Spinner) findViewById(R.id.bannerParitySpinner);
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mParitySpinner.setAdapter(adapter);
tempArray = SlickUSB2Serial.PARITY_OPTIONS;
for (int i = 0; i < tempArray.length; i++) {
adapter.add(tempArray[i]);
}
mParitySpinner.setSelection(SlickUSB2Serial.ParityOption.PARITY_NONE
.ordinal());
mStopSpinner = (Spinner) findViewById(R.id.bannerStopSpinner);
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mStopSpinner.setAdapter(adapter);
tempArray = SlickUSB2Serial.STOP_BITS;
for (int i = 0; i < tempArray.length; i++) {
adapter.add(tempArray[i]);
}
mStopSpinner
.setSelection(SlickUSB2Serial.StopBits.STOP_1_BIT.ordinal());
如果我单击 updateSettings,则运行此代码:
else if (v == mUpdateSettings) {
if (mSelectedAdapter == null) {
return;
}
mSelectedAdapter.setCommSettings(BaudRate.values()[mBaudSpinner
.getSelectedItemPosition()], DataBits.values()[mDataSpinner
.getSelectedItemPosition()],
ParityOption.values()[mParitySpinner
.getSelectedItemPosition()],
StopBits.values()[mStopSpinner.getSelectedItemPosition()]);
}
我正在尝试将这样的偏好用于一个微调器作为示例,它是否在正确的轨道上?
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("savedValue",mBaudSpinner.getSelectedItem().toString());
prefEditor.commit();
String username = prefs.getString("savedValue", String.valueOf(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal()));
mBaudSpinner.setSelection(Integer.parseInt(username));
你是做什么的,我是否将 sharedPrefs 的东西设为全局,以便它可以在 onCreate 和按钮单击侦听器中使用?然后将设置微调器默认值的行更改为以下示例:
String username = prefs.getString("savedValue", String.valueOf(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal()));
mBaudSpinner.setSelection(Integer.parseInt(username));
然后把其他关于在按钮监听器中保存值的东西?我正在尝试这个,但状态没有保存并且总是默认为错误的值。我做的对吗?
编辑
这些行似乎没有正确设置默认值,这是为什么呢?
prefs = PreferenceManager.getDefaultSharedPreferences(this);
String username = prefs.getString("savedValue", Integer.toString(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal()));
mBaudSpinner.setSelection(Integer.parseInt(username));