我需要确保用户不会选择未来的日期作为筛选记录的选择。我可以限制月份,但不能限制未来的日期。请帮忙。
这是我使用的代码:
// create the instance of the calendar to set the
// bounds
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
// now set the starting bound from current month to
// previous MARCH
/*calendar.set(Calendar.MONTH, Calendar.MARCH);
long march = calendar.getTimeInMillis();*/
// now set the ending bound from current month to
// DECEMBER
//calendar.set(Calendar.MONTH, Calendar.DECEMBER);
//long december = calendar.getTimeInMillis();
calendar.set(Calendar.DATE, Calendar.DAY_OF_MONTH);
long currentDateTimeMills = calendar.getTimeInMillis();
// create the instance of the CalendarConstraints
// Builder
CalendarConstraints.Builder calendarConstraintBuilder = new CalendarConstraints.Builder();
// and set the start and end constraints (bounds)
//calendarConstraintBuilder.setStart(march);
//calendarConstraintBuilder.setEnd(december);
calendarConstraintBuilder.setEnd(currentDateTimeMills);
MaterialDatePicker.Builder<Pair<Long, Long>> materialDateBuilder = MaterialDatePicker.Builder.dateRangePicker();
materialDateBuilder.setTitleText("SELECT A DATE");
materialDateBuilder.setCalendarConstraints(calendarConstraintBuilder.build());
//final MaterialDatePicker<Pair<Long, Long>> materialDatePicker = materialDateBuilder.build();
final MaterialDatePicker materialDatePicker = materialDateBuilder.build();
dateRangePickerButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fragmentManager.findFragmentByTag("MATERIAL_DATE_PICKER") == null) {
materialDatePicker.show(((FragmentActivity) v.getContext()).getSupportFragmentManager(), "MATERIAL_DATE_PICKER");
}
}
});
DatePickerDialog dialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
添加此行
dialog.datePicker.maxDate = Date().time
感谢-https://www.geeksforgeeks.org/more-functionalities-of-material-design-date-picker-in-android/?ref=lbp
我能够使用以下代码禁用未来的结果:
CalendarConstraints.Builder calendarConstraintBuilder = new CalendarConstraints.Builder();
calendarConstraintBuilder.setValidator(DateValidatorPointBackward.now());
MaterialDatePicker.Builder<Pair<Long, Long>> materialDateBuilder = MaterialDatePicker.Builder.dateRangePicker();
materialDateBuilder.setTitleText("SELECT DATE RANGE");
materialDateBuilder.setCalendarConstraints(calendarConstraintBuilder.build());
//final MaterialDatePicker<Pair<Long, Long>> materialDatePicker = materialDateBuilder.build();
final MaterialDatePicker materialDatePicker = materialDateBuilder.build();
dateRangePickerButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fragmentManager.findFragmentByTag("MATERIAL_DATE_PICKER") == null) {
materialDatePicker.show(((FragmentActivity) v.getContext()).getSupportFragmentManager(), "MATERIAL_DATE_PICKER");
}
}
});