Setting MinDate on DatePicker moves CalendarView to 1964

后端 未结 5 1697
离开以前
离开以前 2020-12-08 16:40

I\'m debugging an issue where the CalendarView in a DatePicker moves to October 1964 if there is a non-default minimum date set. This reproduces at

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 17:00

    The above answers are right on about the sources of this bug. For my application, I'm using this workaround:

    Every time the DatePicker's date or minDate is changed, call the following routine with the date that should be selected in the picker/calendar:

    private void fixUpDatePickerCalendarView(Calendar date) {
        // Workaround for CalendarView bug relating to setMinDate():
        // https://code.google.com/p/android/issues/detail?id=42750
        // Set then reset the date on the calendar so that it properly
        // shows today's date. The choice of 24 months is arbitrary.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            final CalendarView cal = datePicker.getDatePicker().getCalendarView();
            if (cal != null) {
                date.add(Calendar.MONTH, 24);
                cal.setDate(date.getTimeInMillis(), false, true);
                date.add(Calendar.MONTH, -24);
                cal.setDate(date.getTimeInMillis(), false, true);
            }
        }
    }
    

提交回复
热议问题