我想在DatePickerDialog中禁用今天日期之后的日期

时间:2018-01-08 09:11:46

标签: android

int mMonth = 0, mDay = 0, mYear = 0;
            DatePickerDialog mDatePicker = new DatePickerDialog(activity_registrationactivityy.this, new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                    Calendar mcurrentDate = Calendar.getInstance();
                    int mYear = selectedyear;
                    int mMonth = selectedmonth;
                    int mDay = selectedday;

                    DatePickerDialog dpDialog = new DatePickerDialog(getApplicationContext(), mDatePicker, mYear, mMonth, mDay);
                    dpDialog.getDatePicker().setMaxDate(mcurrentDate.getTimeInMillis());


                    et_birthdate.setText(new StringBuilder().append(mDay).append("/").append(mMonth + 1).append("/").append(mYear));

                    //takes current date
                }
            }, m`enter code here`Year, mMonth, mDay);
            mDatePicker.setTitle("Select date");
            mDatePicker.show();

2 个答案:

答案 0 :(得分:0)

您可以使用方法setMaxDate(long maxDate)

要禁用将来的日期,请使用以下命令:

dpDialog.getDatePicker().setMaxDate(System.currentTimeMillis());

答案 1 :(得分:0)

试试这个

DatePickerDialog dpDialog = new DatePickerDialog(getContext(), myDateListener, mYear, mMonth, mDay);
    dpDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());