单击edittext时,将打开日期选择器对话框。如果没有在日期选择器中单击完成按钮并在对话框外单击,则会在edittext中设置。
我checked this但这是第一次有效。通过选择对话框而不单击“完成”按钮来选择第二次时,它会失败。
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
}
};
};
如何解决这个问题?
答案 0 :(得分:0)
@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())在此条件下添加代码, 这是一个简单的步骤并检查输出