Android DatePicker shows unavailable months when u

2019-04-19 14:35发布

I've only found 1 other instance of this issue on StackOverflow which was unanswered (last year), so I figured I'd give it another shot. (Android DatePicker/Dialog displaying incorrect month/s using min/max date, with an actual image)

When setting the minDate and maxDate of the Android DatePicker, it'll show months that are unavailable within the range of min and max date. I'll demonstrate this issue with the following images:

When I'm at the minDate:

When I'm at minDate

When I'm in between the date limits:

In between dates

When I'm at the maxDate:

At maxDate

The unavailable months (in this case April and June) act as min and max values in this situation, so going to April, the DatePicker will shoot to 15th of May, or trying to slide to June will move the DatePicker to the 22th of May.

Is it possible to keep those (unavailable) months hidden from view, so in this testcase, the only selectable part would be the date? Also keeping in mind that, with an interval between for instance the 29th of May and the 5th of June, June has to appear in the list.

2条回答
男人必须洒脱
2楼-- · 2019-04-19 14:56

OPTION 1. You could use android-times-square

and give in a custom date range so that it fades out the unavailable dates, gives more visual representation too

Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);

CalendarPickerView calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
Date today = new Date();
calendar.init(today, nextYear.getTime())
    .inMode(RANGE);
查看更多
甜甜的少女心
3楼-- · 2019-04-19 15:02

I fixed that issue by resetting the current time to midnight:

Calendar date = Calendar.getInstance();
// reset hour, minutes, seconds and millis
date.set(Calendar.HOUR_OF_DAY, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
datePicker.setMaxDate(date.getTimeInMillis());
查看更多
登录 后发表回答