Java DatePicker MinDate是可选的-Android

Java DatePicker MinDate是可选的-Android,java,android,android-datepicker,Java,Android,Android Datepicker,我已经为我的DatePicker视图设置了一个minDate()。 现在的问题是,我仍然可以从指定的最短日期之前的日期中进行选择 我的java: long thirtyDaysInMilliseconds = 2592000000l; datePicker.setMinDate(System.currentTimeMillis() - thirtyDaysInMilliseconds); // Setting the minimum date 我的XML: <DatePicker

我已经为我的
DatePicker
视图设置了一个
minDate()
。 现在的问题是,我仍然可以从指定的最短日期之前的日期中进行选择

我的java:

long thirtyDaysInMilliseconds = 2592000000l;
datePicker.setMinDate(System.currentTimeMillis() - thirtyDaysInMilliseconds); // Setting the minimum date
我的XML:

 <DatePicker
    android:id="@+id/date_picker_id"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_below="@id/header_id" 

    />

和一张图片来演示:

看,我仍然可以选择1,它不是我分配的
minDate()
的范围(1-9不在范围内,10-13在范围内)。除此之外的圆圈显示它是可选择的。我不想点击那些。我还可以从那些“未选择的日期”中检索信息
这是为什么?我如何修复它?

我的日期选择器也有同样的问题,显示为对话框片段

我注意到这只发生在棒棒糖上

我的解决方案并不完美,但有助于防止超出范围的日期破坏代码,但仍然可以选择:(

所以在日期选择器上设置最小日期和最大日期

    if (mMinDate != null) {
        datePickerDialog.getDatePicker().setMinDate(mMinDate.getTime());
    }

    if (mMaxDate != null) {
        datePickerDialog.getDatePicker().setMaxDate(mMaxDate.getTime());
    }
然后在代码中,从选择器中提取当前日期(在我的例子中是带有OK按钮的对话框),执行此检查

//Just getting the current date from the date picker
    int day = ((DatePickerDialog) dialog).getDatePicker().getDayOfMonth();
                        int month = ((DatePickerDialog) dialog).getDatePicker().getMonth();
                        int year = ((DatePickerDialog) dialog).getDatePicker().getYear();
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(year, month, day);
                        Date date = calendar.getTime(); //This is what we use to compare with. 


 /** Only do this check on lollipop because the native picker has a bug where the min and max dates are ignored */
                    if (Build.VERSION.SDK_INT >= 21) {
                        boolean isDateValid = true; //Start as OK but as we go through our checks this may become false
                        if(mMinDate != null){
                            //Check if date is earlier than min
                            if(date.before(mMinDate)){
                                isDateValid = false;
                            }
                        }

                        if(mMaxDate != null){
                            //Check if date is later than max
                            if(date.after(mMaxDate)){
                                isDateValid = false;
                            }
                        }
                        if(isDateValid){ //if true we can use date, if false do nothing but you can add some else code
                            /** ALL GOOD DATE APPLY CODE GOES HERE */
                        }
                    }else{ //We are not on lollipop so no need for this check
                        /** ALL GOOD DATE APPLY CODE GOES HERE */
                    }