在 Android 日期选择器中禁用未来日期 [英] Disable future dates in Android date picker

查看:26
本文介绍了在 Android 日期选择器中禁用未来日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

谢谢阿什瓦尼

推荐答案

您应该能够调用 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;
}

这篇关于在 Android 日期选择器中禁用未来日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆