Android。日期选择器(微调器)显示不正确
Android. Date Picker (spinner) doesn't display properly
我按如下方式初始化我的日期选择器:
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));
}
}
但最初,布局如下所示:
如果我开始转动日期微调器,7 月 8 日就会出现。
关于为什么会发生这种情况的任何想法?!
我尝试调用 picker.invalidate()
或 picker.requestLayout()
甚至 picker.requestFocus()
但似乎没有任何效果。
检查 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));
}
我按如下方式初始化我的日期选择器:
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));
}
}
但最初,布局如下所示:
如果我开始转动日期微调器,7 月 8 日就会出现。
关于为什么会发生这种情况的任何想法?!
我尝试调用 picker.invalidate()
或 picker.requestLayout()
甚至 picker.requestFocus()
但似乎没有任何效果。
检查 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));
}