Android:日期选择器不应接受当前日期和未来日期 [英] Android: Date Picker Should not accept current date and future dates
本文介绍了Android:日期选择器不应接受当前日期和未来日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何限制日期选择器在 android 中接受当前和未来的日期我正在使用 google api...有什么想法..?
How to restrict date picker from accepting current and future dates in android i am using google api...any idea..?
推荐答案
从 API 级别 11 开始,就有了一种方法:
Since API level 11 there is a method for that:
DatePicker.setMaxDate(long maxDate)
如果它必须在以前的版本中工作,请使用此方法:
If it has to work in previous versions, use this method:
public void init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
您可以传递您自己的 OnDateChangedListener,它将无效日期重置"为最新的有效日期:
You could pass your own OnDateChangedListener which "resets" invalid dates to the newest valid one:
DatePicker picker = ...
int year = ...
int monthOfYear = ...
int dayOfMonth = ...
picker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// check if current date is OK
boolean dateOk = ...
if (!dateOk) {
// correct the date, but be sure the corrected date is OK
// => otherwise you might get endless recursion
year = ...
monthOfYear = ...
dayOfMonth = ...
// update the date widget with the corrected date values
view.updateDate(year, monthOfYear, dayOfMonth);
}
}
});
这篇关于Android:日期选择器不应接受当前日期和未来日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文