Android DatePicker 限制日期选择范围

1. Android DatePicker 设定不能选取晚于今日日期


Calendar calendar = Calendar.getInstance();
			int year = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH);
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			datePicker.init(year, month, day, new OnDateChangedListener() {

				@Override
				public void onDateChanged(DatePicker view, int year,
						int monthOfYear, int dayOfMonth) {

					if (isDateAfter(view)) {
						Calendar mCalendar = Calendar.getInstance();
						view.init(mCalendar.get(Calendar.YEAR),
								mCalendar.get(Calendar.MONTH),
								mCalendar.get(Calendar.DAY_OF_MONTH), this);
					}
				}

				private boolean isDateAfter(DatePicker tempView) {
					Calendar mCalendar = Calendar.getInstance();
					Calendar tempCalendar = Calendar.getInstance();
					tempCalendar.set(tempView.getYear(), tempView.getMonth(),
							tempView.getDayOfMonth(), 0, 0, 0);
					if (tempCalendar.after(mCalendar))
						return true;
					else
						return false;
				}
			});


你可能感兴趣的:(Android)