Creé una DatePickerPreference, es decir, extendí DialogPreference y creé un objeto DatePicker dentro y lo tuve funcionando casi a la perfección. Cambia los valores cuando hace clic en las flechas hacia arriba y hacia abajo, y guarda el valor que seleccione.Custom DatePicker como Preference no mantiene el valor cuando el usuario edita el valor en el campo
Sin embargo, si hace clic dentro del campo y escribe el nuevo valor allí, ¡no guarda el valor actualizado! Al usar las flechas, siempre se llama al método onDateChanged(); cuando el usuario ingresa al campo y lo edita, solo llamará a OnDateChanged si selecciona otro campo (y en este caso, si edita el último campo y simplemente presiona OK, se ignorará la última edición). He descubierto que Eric Besette posted a similar problem with his TimePickerPreference
Aquí está mi código:
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;
}
¿Cuál es la pregunta? (es decir, ¿qué intentas lograr?) – yuku
Cuando el usuario introduce el valor haciendo clic en el campo día/mes/año y escribe el valor correcto, el valor no se actualiza. Solo funciona si el usuario establece la fecha usando las flechas arriba/abajo. Quiero que pueda actualizarse usando las flechas o ingresando el valor directamente en el campo ... – OtavioKR
Esto no lo resuelve, pero no creo que mDate se actualice cuando un usuario cambia la fecha, por eso cuando cierras no está actualizando el valor. – stealthcopter