Android DatePicker/Dialog displaying incorrect month/s using min/max date

隐身守侯 提交于 2019-11-28 04:11:14

问题


I'm using the DatePickerDialog and the DatePicker to set a specific range.

datePicker.setMinDate(minDate);
datePicker.setMaxDate(maxDate);

If my min/max - dates are in the range of one month (eg.: 7.2.2014 - 27.2.2014), i still get the neighbour months displayed in the dialog (Jan, Mar). If i choose the last month (Jan), the dialog automatically swaps to the minDate and the month switches back to Feb. Same thing for the future month.

Is there a way to fix this, so that i only get the months displayed, which are in my range?


回答1:


In Android Month start from zero.....

So u have to use zero for january and 11 for December




回答2:


i have the same bug (and many peoples on StackOverflow)

my solution:

    monthPickerFrom =(DatePicker)mDialogView.findViewById(R.id.monthPickerFrom);
    HideDay(monthPickerFrom);
    Calendar currentDate = Calendar.getInstance();
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, 2007);
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    monthPickerFrom.setMinDate(cal.getTimeInMillis());

    cal.set(Calendar.YEAR, currentDate.get(Calendar.YEAR));
    cal.set(Calendar.MONTH, currentDate.get(Calendar.MONTH));
    monthPickerFrom.setMaxDate(cal.getTimeInMillis());

    monthPickerFrom.init(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), 1, new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            ...
    });


来源:https://stackoverflow.com/questions/21779855/android-datepicker-dialog-displaying-incorrect-month-s-using-min-max-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!