使用setMaxDate限制DatePickerDialog上可选择的天数后,最后一天不会显示为灰色但不可选。感谢您的帮助。
private void showDatePickerDialog() {
DateTime dateTime = new DateTime();
DateTime tomorrow = dateTime.plusDays(1);
int year = tomorrow.getYear();
int month = tomorrow.getMonthOfYear() -1; // zero based months
int day = tomorrow.getDayOfMonth();
DateTime thirtyDaysInFuture = dateTime.plusDays(30);
long tomorrowMilliseconds = tomorrow.getMillis();
long futureMilliseconds = thirtyDaysInFuture.getMillis();
Log.d(TAG, "YEAR: " + year + ", MONTH: " + month + ", DAY: " + day);
DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Log.d(TAG, i + ", " + i1 + ", " + i2);
}
}, year, month, day);
datePickerDialog.getDatePicker().setMinDate(tomorrowMilliseconds);
datePickerDialog.getDatePicker().setMaxDate(futureMilliseconds);
datePickerDialog.show();
}
答案 0 :(得分:15)
我有类似的问题,我通过将最大日期的小时,分钟和秒分别设置为23,59和59来解决它。
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Add your code here
}
}, year, month, day);
Calendar maxDate = Calendar.getInstance();
maxDate.set(Calendar.HOUR_OF_DAY, 23);
maxDate.set(Calendar.MINUTE, 59);
maxDate.set(Calendar.SECOND, 59);
datePickerDialog.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
答案 1 :(得分:0)
我有同样的问题,我找到了答案。尝试使用此代码:
首先你应该重置当前日期,它是小时,分钟和秒
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.DAY_OF_YEAR, 14); //I set max date +2 weeks
calendar.add(Calendar.HOUR_OF_DAY, 23);
calendar.add(Calendar.MINUTE, 59);
calendar.add(Calendar.SECOND, 59);
dlgDate.getDatePicker().setMaxDate(calendar.getTimeInMillis());
希望我帮助你