在 datepicker 中设置最大日期时能够选择未来日期 [英] Able to choose future date when setting max date in datepicker

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

问题描述

我的目标是使用 AppCompat v21 构建 API 级别 21.

I'm targeting for and building with API level 21, using AppCompat v21.

它给了我一个漂亮的新日期选择器,它有一个意想不到的特性,当 最大日期 已使用

It gives me a nicely looking new date picker, which has the unexpected property of allowing me to choose a future date when max date has been set using

datePicker.setMaxDate(Calendar.getInstance().getTimeInMillis())

未来的日期是灰色的,但我仍然可以选择其中的任何一个.这是一个错误吗?我做错了吗?有没有办法阻止用户在日期选择器中选择未来的日期?

The future dates are greyed out, but I can still choose any one of them. Is that a bug? Am I doing it wrong? Is there a way to prevent the user from being able to pick a future date in the date picker?

旧的 Holo 日期选择器在设置最大日期时不允许选择未来的日期.

The old Holo date picker did not allow picking a future date when setting a maximum date.

更新:

虽然它在我的运行库存 5.0 的 Nexus 4 上不能正常工作,但它在我运行库存 Android 5.1.1 的 Nexus 6 上正常工作.也许这是 Android 5.0 中的一个错误并在 5.1 中修复?谁能确认一下?

While it is not working properly on my Nexus 4 running stock 5.0, it is working properly on my Nexus 6 running stock Android 5.1.1. Perhaps it was a bug in Android 5.0 and it was fixed in 5.1? Can anyone confirm?

推荐答案

所以来回答我自己的问题:

So to answer my own question:

我已经在 grepcode 中查看了 DatePickerCalendarDelegate.java 的 Android 源代码,特别是在 public void setMaxDate(long maxDate) 中对于两个 Android 版本 5.05.1.

I've looked at the Android source of DatePickerCalendarDelegate.java at grepcode, specifically at public void setMaxDate(long maxDate) for both Android versions 5.0 and 5.1.

5.1 中 setMaxDate() 的新功能是

What's new in 5.1 in setMaxDate() is that

mDayPickerView.goTo(getSelectedDay(), false, true, true);

已更改为:

mDayPickerView.setMaxDate(maxDate);

似乎他们在那里修复了它,这与我的观察相符,即它在 5.1 中按预期工作,但在 5.0 中却没有.

It seems they fixed it there, which corresponds with my observation that it works as expected in 5.1 but not in 5.0.

因此,我们似乎一直在处理它在 Android 5.0 中无法正常工作的事实(最大日期后的天数显示为灰色,但仍然可以选择).

And so it seems we're stuck with dealing with the fact that it doesn't work properly in Android 5.0 (days after max date are greyed out but can still be chosen).

这篇关于在 datepicker 中设置最大日期时能够选择未来日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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