自定义的DatePicker为preference不保留值时的用户编辑值字段 [英] Custom DatePicker as Preference does not keep value when user edits value in field

查看:144
本文介绍了自定义的DatePicker为preference不保留值时的用户编辑值字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个datepicker preference,即我伸出对话框preference创造一个datepicker对象中,把它的工作近乎完美。当您单击向上和向下箭头,并节省您所选择的价值它改变了值。

但是,如果您单击该字段,类型新的值出现在里面,它不保存更新的价值!当使用箭头时,onDateChanged()方法总是调用;当用户进入该领域,并编辑它,它只会调用onDateChanged如果他选择另一场(在这种情况下,如果他编辑的最后一个字段,只是点击确定,最后的编辑将被忽略)。我发现,埃里克Besette发布了类似的问题,他TimePicker preference

下面是我的code:

 
    公共类的DatePicker preference扩展对话框preference工具
OnDateChangedListener,OnDateSetListener {

    @覆盖
    受保护的视图onCreateDialogView(){
        DatePicker的选择器=新的DatePicker(的getContext());
        mDate = getPersistedLong(System.currentTimeMillis的());

        台历挂历= Calendar.getInstance();
        calendar.setTimeInMillis(mDate);

        INT天= calendar.get(Calendar.DAY_OF_MONTH);
        INT月= calendar.get(Calendar.MONTH);
        INT年= calendar.get(Calendar.YEAR);

        picker.init(年,月,日,本);
        返回选取器;
    }

    公共无效onDateChanged(DatePicker的观点,年整型,诠释monthOfYear,
            INT DAYOFMONTH){
        mDate =(新日期(年 -  1900年,monthOfYear,DAYOFMONTH))的getTime()。
    }

    公共无效onDateSet(DatePicker的观点,年整型,诠释monthOfYear,
            INT DAYOFMONTH){
        onDateChanged(查看,一年,monthOfYear,DAYOFMONTH);
    }

    @覆盖
    公共无效setDefaultValue(对象设置defaultValue){
        super.setDefaultValue(将String.valueOf((
            新的日期(将String.valueOf(设置defaultValue)))的getTime()))。
    }

    @覆盖
    保护无效onDialogClosed(布尔positiveResult){
        super.onDialogClosed(positiveResult);

        如果(positiveResult){
            如果(isPersistent()){
                persistLong(mDate);
            }
            callChangeListener(将String.valueOf(mDate));
        }
    }

    公众诠释了getYear(){} /*(...)*/
    公众诠释的getMonth(){} /*(...)*/
    公众诠释getDay(){} /*(...)*/

    公众的DatePicker preference(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        在里面();
    }

    公众的DatePicker preference(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        在里面();
    }

    公共无效的init(){setPersistent(真正的); }
    公共无效的setDate(日期日期){mDate = date.getTime(); }
    公共无效的setDate(长milisseconds){mDate = milisseconds; }

    公共字符串GETDATE(INT风格){
        返回DateFormat.getDateInstance(风格).format(新日期(mDate));
    }

    众长GETDATE(){返回mDate; }

    专用长mDate;
    公共静态最终诠释DATE_SHORT = DateFormat.SHORT;
    公共静态最终诠释DATE_MEDIUM = DateFormat.MEDIUM;
    公共静态最终诠释DATE_LONG = DateFormat.LONG;
    公共静态最终诠释DATE_FULL = DateFormat.FULL;
    公共静态最终诠释DATE_DEFAULT = DateFormat.DEFAULT;
}
 

解决方案

由于此类扩展对话preference 类,它已经处理修改共享preference 使用按钮。要在用户键入正确后,更新日期变量的新日期,需要手动更新共享preference。

您可以做到这一点,如下所示:

 共享preferences preFS = preferenceManager.getDefaultShared preferences(本);
共享preferences.Editor preferences = prefs.edit();
preferences.putLong(mdate_key,mDate.getTime());
preferences.commit();
 

下面,mdate_key将对应于 preferenceActivity XML使用了的DatePicker preference 键文件。

我建议无论是在 onDateChanged这样做()的onDestroy()

I created a DatePickerPreference, i.e. I extended DialogPreference and created a DatePicker object inside and had it working almost perfectly. It changes values when you click the arrows up and down and saves the value you select.

However, if you click inside the field and types the new value there, it doesn't save the updated value! When using the arrows, the onDateChanged() method is always called; when user enters the field and edits it, it will only call onDateChanged if he selects another field (and in this case, if he edits the last field and just hit OK, the last edit will be ignored). I found that Eric Besette posted a similar problem with his TimePickerPreference

Here is my code:


    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;
}

解决方案

Since this class extends the DialogPreference class, it already handles changing the SharedPreference using the buttons. To correctly update your date variable after the user types the new date, you need to update the SharedPreference manually.

You can do this as follows:

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

Here, mdate_key will correspond to the DatePickerPreference key used in the PreferenceActivity XML file.

I recommend either doing this in onDateChanged() or onDestroy().

这篇关于自定义的DatePicker为preference不保留值时的用户编辑值字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆