Disable future dates in Android date picker

后端 未结 1 887
有刺的猬
有刺的猬 2020-12-11 17:05

Hello All: How to disable future dates in DatePickerDialog in Android.

I am using following implementation. http://www.androidpeople.com/android-datepicker-dialog-ex

相关标签:
1条回答
  • 2020-12-11 18:05

    You should be able to call getDatePicker().setMaxDate(long) on your DatePickerDialog to set today as your max date. You can update the function with the same name from the snippet you posted.

    Note the DatePickerDialog is the object that I referenced in the Android Docs from the link I posted.

    @Override
    protected Dialog onCreateDialog(int id) {
        Calendar c = Calendar.getInstance();
        int cyear = c.get(Calendar.YEAR);
        int cmonth = c.get(Calendar.MONTH);
        int cday = c.get(Calendar.DAY_OF_MONTH);
        switch (id) {
            case DATE_DIALOG_ID:
            //start changes...
            DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
            dialog.getDatePicker().setMaxDate(new Date().getTime());
            return dialog;
            //end changes...
        }
        return null;
    }
    
    0 讨论(0)
提交回复
热议问题