设置 Android 日期选择器日期限制

发布于 2024-08-26 12:34:24 字数 167 浏览 10 评论 0原文

我在 android 中使用 datePicker 根据用户选择的日期显示图像。我需要将所述日期限制为某些日期,例如 2010 年 1 月 1 日至 2010 年 12 月 31 日。我想这很简单,但我在哪里可以找到有关如何限制这些日期的答案。

有谁知道如何限制Android DatePicker的日期

I am using datePicker in android to display images based on user selected dates. I need to limit said dates to certain days for instance Jan 1st 2010 to Dec 31st 2010. Simple as that i thought but no where can i find the answer on how to limit these dates.

Does anyone know how to limit the dates for Android DatePicker

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心欲静而疯不止 2024-09-02 12:34:25

您只需设置 : ,

dateDialog.getDatePicker().setMaxDate(new Date().getTime());

其中

dateDialog.getDatePicker().setMinDate(new Date().getTime());

dateDialog 是 a

new DatePickerDialog()

,为 MaxDate 和 MinDate 设置的参数类型是 long

You can just set :

dateDialog.getDatePicker().setMaxDate(new Date().getTime());

and

dateDialog.getDatePicker().setMinDate(new Date().getTime());

where dateDialog is a

new DatePickerDialog()

and the param type to set for MaxDate and MinDate is a long

回忆凄美了谁 2024-09-02 12:34:25

现在回答可能已经太晚了,但是为 DatePickerDialog 设置 minmax 日期很容易。 适用于所有 Android 版本。只需根据Android版本返回DatePickerDialog即可

protected Dialog onCreateDialog(int id)
{
    switch ( id )
    {
        case DATE_DIALOG_ID:
            DatePickerDialog dialog = null;
            if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
            {
                dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
                {
                    @Override
                    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDate)
                    {
                        mTextView.setText(selectedDate + "/" + selectedMonth + 1 + "/" + selectedYear);
                    }
                }, year, month - 1, date);
                Calendar currentDate = Calendar.getInstance();
                dialog.getDatePicker().setMaxDate(currentDate.getTimeInMillis());
                //If you need you can set min date too
            }
            else
            {
                dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
                {
                    @Override
                    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDate)
                    {
                        mTextView.setText(selectedDate + "/" + selectedMonth + 1 + "/" + selectedYear);
                    }
                }, year, month - 1, date)
                {
                    @Override
                    public void onDateChanged(DatePicker view, int year, int month, int day)
                    {
                        if ( year <= maxYear && month + 1 <= maxMonth && date <= maxDate ) //meets your criteria
                        {
                            view.updateDate(year, month, date); //update the date picker to selected date
                        }
                        else
                        {
                            view.updateDate(maxYear, maxMonth - 1, maxDate); // or you update the date picker to previously selected date
                        }
                    }
                };
            }
            return dialog;
    }
    return null;
}

It might be too late to answer, but the it is easy to set the min and max date for DatePickerDialog. Works for All Android versions. Simply you need to return the DatePickerDialog based on Android version

protected Dialog onCreateDialog(int id)
{
    switch ( id )
    {
        case DATE_DIALOG_ID:
            DatePickerDialog dialog = null;
            if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
            {
                dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
                {
                    @Override
                    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDate)
                    {
                        mTextView.setText(selectedDate + "/" + selectedMonth + 1 + "/" + selectedYear);
                    }
                }, year, month - 1, date);
                Calendar currentDate = Calendar.getInstance();
                dialog.getDatePicker().setMaxDate(currentDate.getTimeInMillis());
                //If you need you can set min date too
            }
            else
            {
                dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
                {
                    @Override
                    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDate)
                    {
                        mTextView.setText(selectedDate + "/" + selectedMonth + 1 + "/" + selectedYear);
                    }
                }, year, month - 1, date)
                {
                    @Override
                    public void onDateChanged(DatePicker view, int year, int month, int day)
                    {
                        if ( year <= maxYear && month + 1 <= maxMonth && date <= maxDate ) //meets your criteria
                        {
                            view.updateDate(year, month, date); //update the date picker to selected date
                        }
                        else
                        {
                            view.updateDate(maxYear, maxMonth - 1, maxDate); // or you update the date picker to previously selected date
                        }
                    }
                };
            }
            return dialog;
    }
    return null;
}
余生共白头 2024-09-02 12:34:25

我想你必须实现自己的DatePicker。其背后的逻辑是:“只有少数开发人员会限制 DatePicker,因此他们必须自己实现该功能。”

I guess you have to implement your own DatePicker. The logic behind this would be: "Only a few developers would ever limit the DatePicker so they have to implement the functionality themselfs."

做个少女永远怀春 2024-09-02 12:34:25

为什么你不应该使用自定义选项设计像 DatePicker 这样的新布局。您可以使用 Spinners 和 Forloops 来完成。

Why you should not design new layout like DatePicker with customoption. You can do with Spinners and Forloops.

请你别敷衍 2024-09-02 12:34:25

你可以通过输入简单的一行来实现这一点

mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
        mcurrentDate.add(Calendar.MONTH,6);
        mDatePicker.getDatePicker().setMaxDate(mcurrentDate.getTimeInMillis());

U can achieve this by typing a simple line

mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
        mcurrentDate.add(Calendar.MONTH,6);
        mDatePicker.getDatePicker().setMaxDate(mcurrentDate.getTimeInMillis());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文