在Android日期选择器中停用未来日期

Ash*_*i K 7 android datepicker

大家好:如何在Android中禁用DatePickerDialog中的未来日期.

我正在使用以下实现. http://www.androidpeople.com/android-datepicker-dialog-example

谢谢Ashwani

nic*_*ild 20

你应该能够调用getDatePicker().在DatePickerDialog上设置setMaxDate(long),将今天设置为最大日期.您可以使用您发布的代码段中的相同名称更新该功能.

请注意,DatePickerDialog是我在Android文档中从我发布的链接中引用的对象.

@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;
}
Run Code Online (Sandbox Code Playgroud)

  • Nicholas,getDatePicker()方法在API级别11中可用,但我的应用程序是针对API 7+,有没有办法在API级别7中执行此操作? (4认同)