简体   繁体   中英

Android: Date Picker Should not accept current date and future dates

如何限制日期选择器不接受android中的当前和将来的日期,我正在使用Google api ...任何想法..?

  1. Since API level 11 there is a method for that:

     DatePicker.setMaxDate(long maxDate) 
  2. If it has to work in previous versions, use this method:

     public void init(int year, int monthOfYear, int dayOfMonth, DatePicker.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);
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM