我创建了一个 DatePickerPreference,即我扩展了 DialogPreference 并在其中创建了一个 DatePicker 对象,并且几乎完美地工作。当您单击上下箭头并保存您选择的值时,它会更改值。
但是,如果您在该字段内单击并在其中键入新值,它不会保存更新后的值!使用箭头时,总是调用 onDateChanged() 方法;当用户输入该字段并对其进行编辑时,如果他选择另一个字段,它只会调用 onDateChanged(在这种情况下,如果他编辑最后一个字段并点击确定,最后的编辑将被忽略)。我发现 Eric Besette posted a similar problem with his TimePickerPreference
这是我的代码:
public class DatePickerPreference extends DialogPreference implements
OnDateChangedListener, OnDateSetListener {
@Override
protected View onCreateDialogView() {
DatePicker picker = new DatePicker(getContext());
mDate = getPersistedLong(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(mDate);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
picker.init(year, month, day, this);
return picker;
}
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mDate = (new Date(year - 1900, monthOfYear, dayOfMonth)).getTime();
}
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
onDateChanged(view, year, monthOfYear, dayOfMonth);
}
@Override
public void setDefaultValue(Object defaultValue) {
super.setDefaultValue(String.valueOf((
new Date(String.valueOf(defaultValue))).getTime()));
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if(positiveResult) {
if(isPersistent()) {
persistLong(mDate);
}
callChangeListener(String.valueOf(mDate));
}
}
public int getYear() { /*(...)*/ }
public int getMonth() { /*(...)*/ }
public int getDay() { /*(...)*/ }
public DatePickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DatePickerPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init() { setPersistent(true); }
public void setDate(Date date) { mDate = date.getTime(); }
public void setDate(long milisseconds) { mDate = milisseconds; }
public String getDate(int style) {
return DateFormat.getDateInstance(style).format(new Date(mDate));
}
public long getDate() { return mDate; }
private long mDate;
public static final int DATE_SHORT = DateFormat.SHORT;
public static final int DATE_MEDIUM = DateFormat.MEDIUM;
public static final int DATE_LONG = DateFormat.LONG;
public static final int DATE_FULL = DateFormat.FULL;
public static final int DATE_DEFAULT = DateFormat.DEFAULT;
}
最佳答案
由于此类扩展了 DialogPreference
类,它已经处理了使用按钮更改 SharedPreference
的问题。要在用户键入新日期后正确更新日期变量,您需要手动更新 SharedPreference
。
您可以按如下方式执行此操作:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor preferences = prefs.edit();
preferences.putLong("mdate_key", mDate.getTime());
preferences.commit();
在这里,mdate_key 将对应于 PreferenceActivity
XML 文件中使用的 DatePickerPreference
键。
我建议在 onDateChanged()
或 onDestroy()
中执行此操作。
关于android - 当用户在字段中编辑值时,自定义 DatePicker 作为首选项不会保留值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4250903/