如何限制日期选择器接受android中的当前和未来日期我正在使用google api ...任何想法..?
答案 0 :(得分:3)
从API级别11开始,有一种方法:
DatePicker.setMaxDate(long maxDate)
如果必须在以前的版本中使用,请使用此方法:
public void init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
您可以将自己的OnDateChangedListener“重置”为最新的有效日期:
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);
}
}
});