代码之家  ›  专栏  ›  技术社区  ›  AndreiBogdan

安卓。日期选择器(微调器)显示不正确

  •  0
  • AndreiBogdan  · 技术社区  · 7 年前

    我初始化日期选择器如下:

        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));
            }
        }
    

    但最初,布局看起来是这样的:

    enter image description here

    如果我开始转动日间旋转器,7月8日就会出现。 enter image description here

    你知道为什么会这样吗?啊!

    我打过电话 picker.invalidate() picker.requestLayout() 甚至 picker.requestFocus() 但似乎什么都没用。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Pankaj Kumar    7 年前

    检查的值 startWithDate.get(Calendar.DAY_OF_MONTH) 是的。它可能对应用于日期选择器的情况无效,例如max range。

    为了正确处理它,您可以在输入超出范围时显示最小日期或最大日期。

    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));
    }