问题
When I click edittext, date picker dialog opens. without clicking done button in date picker and clicked outside of dialog, it sets in edittext.
I checked this but this works for first time. When selecting second time by choosing dialog and without clicking done button, it fails.
private final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
private boolean fired;
public void resetFired(){
fired = false;
}
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
if (fired) {
Log.e("DatePicker", "Ignoring for first time");
birthDayValue = selectedDay;
birthMonthValue = selectedMonth;
birthYearValue = selectedYear;
String birthDayDateFormt = Utils.getBirthDayDate(selectedYear,
selectedMonth, selectedDay);
dateOfBirth.setText(birthDayDateFormt);
dateOfBirth.setError(null);
return;//ignore and return.
}
fired = true;//first time fired
}
};
};
How to solve this issue?
回答1:
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
if(view.isShown()) {
if (fired) {
Log.e("DatePicker", "Ignoring for first time");
birthDayValue = selectedDay;
birthMonthValue = selectedMonth;
birthYearValue = selectedYear;
String birthDayDateFormt = Utils.getBirthDayDate(selectedYear,
selectedMonth, selectedDay);
dateOfBirth.setText(birthDayDateFormt);
dateOfBirth.setError(null);
return;//ignore and return.
}
fired = true;//first time fired
}
}
if(view.isShown()) add code in this condition, its a simple step do it and check output
来源:https://stackoverflow.com/questions/28262439/how-to-handle-date-picker-dialog-not-to-set-in-edittext-when-clicked-outside-of