第二次打开时从数字选择器中突出显示所选项目
Highlight selected item from number picker when it will open second time
我有数字选择器,我正在用字符串数组填充它。
final String[] power = {"0.00", "0.25", "0.50", "0.75","1.00"};
npPowerSecond.setMinValue(0);
npPowerSecond.setMaxValue(power.length - 1);
npPowerSecond.setDisplayedValues(power);
我显示的值如上。当我打开数字选择器时,它会显示“0.00”作为选定值。现在我选择“0.50”。现在,当我打开数字选择器时,它应该显示“0.50”作为所选值。
你能帮帮我吗?我怎样才能做到这一点?
npPowerSecond.setValue(2);
使用setvalue来设置。
npPowerSecond.setValue(selectedNumber);
选择值后,将值设置为该数字选择器。我想它会对你有所帮助。
您必须将 NumberPicker
最后选择的位置保存到 SharedPreferences
。之后,每次打开NumberPicker
,都应该使用void setValue (int value)
为其设置默认选择位置
final String[] power = { "0.00", "0.25", "0.50", "0.75s", "1.00s" };
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(power.length - 1);
numberPicker.setDisplayedValues(power);
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// any time your number picker change, we will save it to SharedPreferences
saveIntToSharedPreferences(mContext, PREF_NUMBER_PICKER_LAST_SELECTED_POSITION, newVal);
}
});
// when we open screen, we will select the last selected value by use setValue(...)
numberPicker.setValue(
getIntFromSharedPreferences(mContext, PREF_NUMBER_PICKER_LAST_SELECTED_POSITION));
来自 SharedPreferences
的 save/get 整数的辅助函数
private void saveIntToSharedPreferences(Context context, String key, int value) {
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
}
private int getIntFromSharedPreferences(Context context, String key) {
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
return sharedPreferences.getInt(key, 0);
}
我有数字选择器,我正在用字符串数组填充它。
final String[] power = {"0.00", "0.25", "0.50", "0.75","1.00"};
npPowerSecond.setMinValue(0);
npPowerSecond.setMaxValue(power.length - 1);
npPowerSecond.setDisplayedValues(power);
我显示的值如上。当我打开数字选择器时,它会显示“0.00”作为选定值。现在我选择“0.50”。现在,当我打开数字选择器时,它应该显示“0.50”作为所选值。
你能帮帮我吗?我怎样才能做到这一点?
npPowerSecond.setValue(2);
使用setvalue来设置。
npPowerSecond.setValue(selectedNumber);
选择值后,将值设置为该数字选择器。我想它会对你有所帮助。
您必须将 NumberPicker
最后选择的位置保存到 SharedPreferences
。之后,每次打开NumberPicker
,都应该使用void setValue (int value)
final String[] power = { "0.00", "0.25", "0.50", "0.75s", "1.00s" };
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(power.length - 1);
numberPicker.setDisplayedValues(power);
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// any time your number picker change, we will save it to SharedPreferences
saveIntToSharedPreferences(mContext, PREF_NUMBER_PICKER_LAST_SELECTED_POSITION, newVal);
}
});
// when we open screen, we will select the last selected value by use setValue(...)
numberPicker.setValue(
getIntFromSharedPreferences(mContext, PREF_NUMBER_PICKER_LAST_SELECTED_POSITION));
来自 SharedPreferences
private void saveIntToSharedPreferences(Context context, String key, int value) {
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
}
private int getIntFromSharedPreferences(Context context, String key) {
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
return sharedPreferences.getInt(key, 0);
}