我按如下所示初始化日期选择器:
if (question.getAnswers().size() > 0) {
EBotAnswer ans = question.getAnswers().get(0);
try {
if (ans.hasMinDate()) {
picker.setMinDate(ans.getMinDateInLocalDate().getTimeInMillis());
}
} catch (IllegalArgumentException e) {
}
try {
if (ans.hasMaxDate()) {
picker.setMaxDate(ans.getMaxDateInLocalDate().getTimeInMillis());
}
} catch (IllegalArgumentException e) {
}
Calendar startWithDate = null;
if (ans.hasStartWithDate()) {
startWithDate = ans.getStartWithDateInLocalDate();
} else if (ans.defaultCalendarDateMin()) {
startWithDate = ans.getMinDateInLocalDate();
} else if (ans.defaultCalendarDateMax()) {
startWithDate = ans.getMaxDateInLocalDate();
} else if (ans.defaultCalendarDateStartWith()) {//This is somewhat redundant
startWithDate = ans.getStartWithDateInLocalDate();
}
if (startWithDate != null) {
picker.updateDate(
startWithDate.get(Calendar.YEAR),
startWithDate.get(Calendar.MONTH),
startWithDate.get(Calendar.DAY_OF_MONTH));
}
}
但是最初,布局看起来像这样:
为什么会这样?
我尝试致电picker.invalidate()
或picker.requestLayout()
甚至是picker.requestFocus()
,但似乎没有任何作用。
答案 0 :(得分:1)
检查startWithDate.get(Calendar.DAY_OF_MONTH)
的值。对于您应用于DatePicker的情况,例如最大范围,可能无效。
为正确处理,您可以显示最小日期或最大日期,以防输入超出范围
if (startWithDate != null
&& (startWithDate.getTimeInMillis() < picker.getMaxDate())
&& (startWithDate.getTimeInMillis() > picker.getMinDate())) {
picker.updateDate(
startWithDate.get(Calendar.YEAR),
startWithDate.get(Calendar.MONTH),
startWithDate.get(Calendar.DAY_OF_MONTH));
} else {
// In case of invalid date set it to minimum
startWithDate.setTimeInMillis(picker.getMinDate());
// Or if you want to set it to maximum
// startWithDate.setTimeInMillis(picker.getMaxDate());
picker.updateDate(
startWithDate.get(Calendar.YEAR),
startWithDate.get(Calendar.MONTH),
startWithDate.get(Calendar.DAY_OF_MONTH));
}