禁用Android的日期选择将来的日期日期、将来、Android

2023-09-06 13:13:06 作者:灵活的小胖纸.

您好所有: 如何禁用DatePickerDialog将来的日期在Android中。

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

我使用下面的实现。 http://www.androidpeople.com/android-datepicker-dialog-example

谢谢 Ashwani

Thanks Ashwani

推荐答案

您应该能够调用getDatePicker()。setMaxDate(long)您DatePickerDialog今天设置你的最大日期。您可以更新从您发布的代码片段中的相同名称的功能。

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.

请注意的DatePickerDialog是我在Android文档中引用从我张贴的链接的对象。

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;
}