当用户在字段中编辑值时,自定义DatePicker作为首选项不会保留值

Ota*_*oKR 6 android datepicker preference

我创建了一个DatePickerPreference,即我扩展了DialogPreference并在其中创建了一个DatePicker对象并让它几乎完美地工作.当您单击上下箭头并保存所选值时,它会更改值.

但是,如果您在字段内单击并在那里键入新值,则不会保存更新的值!使用箭头时,始终调用onDateChanged()方法; 当用户进入该字段并对其进行编辑时,如果他选择另一个字段,它将仅调用onDateChanged(在这种情况下,如果他编辑最后一个字段并且只是点击OK,则将忽略最后一次编辑).我发现Eric Besette在他的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;
}
Run Code Online (Sandbox Code Playgroud)

Phi*_*hil 1

由于此类扩展了该类,因此它已经处理使用按钮的DialogPreference更改。SharedPreference要在用户键入新日期后正确更新日期变量,您需要SharedPreference手动更新。

您可以按如下方式执行此操作:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor preferences = prefs.edit();
preferences.putLong("mdate_key", mDate.getTime());
preferences.commit();
Run Code Online (Sandbox Code Playgroud)

这里,mdate_key 将对应于XML 文件DatePickerPreference中使用的密钥PreferenceActivity

我建议在onDateChanged()或中执行此操作onDestroy()