在DatePickerDialog android上设置MinDate的问题

时间:2015-04-21 12:58:42

标签: android android-activity datepicker android-datepicker datepickerdialog

我在Android中使用DatePickerDialog选择日期范围,我可以在minDate中设置MaxDateLolliPop (5+)。但是在Lollipop下面执行同样的操作时,它会抛出异常。

使用的代码:

Calendar cal_currentDate=Calendar.getInstance();

cal_currentDate.set(cal_currentDate.get(Calendar.YEAR),cal_currentDate.get(Calendar.MONTH)+1,cal_currentDate.get(Calendar.DAY_OF_MONTH));
Calendar cal = Calendar.getInstance();

DatePickerDialog datepicker=new DatePickerDialog(getActivity(),new OnDateSetListener() {
     @Override
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {}                         },
          cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
          datepicker.show();
          datepicker.getDatePicker().setMaxDate(cal.getTimeInMillis()); // here am setting Maximum date as current date
          cal.add(Calendar.MONTH,-3); // here am setting Minimum date 3 months less than current date         
          datepicker.getDatePicker().setMinDate(cal.getTimeInMillis());
          datepickerdialog.show();

结果:Android Lollipop正常运行。 在其他较低版本中出现异常

这是例外消息

 java.lang.IllegalArgumentException: Time not between Sat Mar 21 18:23:16 GMT+00:00 2015 and Tue Apr 21 18:23:16 GMT+00:00 2015
            at android.widget.CalendarView.goTo(CalendarView.java:789)
            at android.widget.CalendarView.setMinDate(CalendarView.java:478)
            at android.widget.DatePicker.setMinDate(DatePicker.java:316)
            at com.teledna.moby.ui.Fragment_homeMenu$16.onCheckedChanged(Fragment_homeMenu.java:1124

请帮忙......

2 个答案:

答案 0 :(得分:0)

setMaxDate

在Honeycomb之下不起作用。

答案 1 :(得分:0)

扩展DatePickerDialog类以支持API级别11之前的Android版本。

检查链接: How to make a Custom DatePicker control with maximum and minimum date range in Android application