MaterialDatePicker Select 仅限 2 个日期之间的日期

MaterialDatePicker Select Date between 2 dates only

如何向 MaterialDatePicker 添加约束以强制用户 select 仅在 2 个日期之间的日期?

val constraintBuilder = CalendarConstraints.Builder()
    val materialRangeBuilder = MaterialDatePicker.Builder.datePicker()
    materialRangeBuilder.setTitleText("Book Date")
    materialRangeBuilder.setCalendarConstraints(constraintBuilder.build())

    val materialRangePicker = materialRangeBuilder.build()

    materialRangePicker.addOnPositiveButtonClickListener {
        Timber.d("$it")
    }

    materialRangePicker.show(supportFragmentManager, "DATE_RANGE_PICKER")

您可以使用 DateValidator 来限制选择。
特别是您可以使用内置验证器:

  • DateValidatorPointForward 启用从给定点向前的日期
  • DateValidatorPointBackward 仅启用给定点之前的日期。

类似于:

MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();

CalendarConstraints.Builder constraintsBuilderRange = new CalendarConstraints.Builder();

//....define min and max for example with LocalDateTime and ZonedDateTime or Calendar

CalendarConstraints.DateValidator dateValidatorMin = DateValidatorPointForward.from(min.getTimeInMillis());
CalendarConstraints.DateValidator dateValidatorMax = DateValidatorPointBackward.before(max.getTimeInMillis());

ArrayList<CalendarConstraints.DateValidator> listValidators =
            new ArrayList<CalendarConstraints.DateValidator>();
listValidators.add(dateValidatorMin);
listValidators.add(dateValidatorMax);
CalendarConstraints.DateValidator validators = CompositeDateValidator.allOf(listValidators);
constraintsBuilderRange.setValidator(validators);

builder.setCalendarConstraints(constraintsBuilderRange.build());

MaterialDatePicker<Long> picker = builder.build();
picker.show(getSupportFragmentManager(), picker.toString());

使用这两种方法

public void setMinDate (long minDate)
public void setMaxDate (long maxDate)

这是官方文档。 https://developer.android.com/reference/android/widget/DatePicker#setMaxDate(long)

例子

val calender = Calendar.getInstance()
val year = calender.get(Calendar.YEAR)
val month = calender.get(Calendar.MONTH)
val day = calender.get(Calendar.DAY_OF_MONTH)
materialRangeBuilder.setMinDate(calender.getTimeInMillis())
materialRangeBuilder.setMaxDate(calender.getTimeInMillis())

@GabrieleMariotti 回答很好,但是 DateValidatorPointBackwards class 不在 material 1.2 版之前的设计库中。如果您想在两个日期之间进行选择并且您有 material 设计库版本 1.1 或更早版本,您可以这样做:

val datePickerBuilder = MaterialDatePicker.Builder.datePicker()
datePickerBuilder.setCalendarConstraints(
    CalendarConstraints.Builder()
        .setValidator(object : CalendarConstraints.DateValidator {
            override fun isValid(date: Long): Boolean {
                return date >= startDate && date < endDate
            }

            override fun writeToParcel(p0: Parcel?, p1: Int) {}

            override fun describeContents(): Int {return 0}
        }).build()
)
val datePicker = datePickerBuilder.build()
datePicker!!.show(fragmentManager, "date picker")

其中 startDate 和 endDate 是长值

这是 kotlin 版本:

private fun calendarConstraints(): CalendarConstraints {
    val dateValidatorMin: DateValidator = DateValidatorPointForward.from(min.getTimeInMillis())
    val dateValidatorMax: DateValidator = DateValidatorPointBackward.before(max.getTimeInMillis())

    val listValidators = ArrayList<DateValidator>()
    listValidators.apply {
        add(dateValidatorMin)
        add(dateValidatorMax)
    }
    val validators = CompositeDateValidator.allOf(listValidators)

    return CalendarConstraints.Builder()
        .setValidator(validators)
        .build()
}
private fun setupCalendar() {
   val materialDatePickerBuilder = MaterialDatePicker.Builder.datePicker().setCalendarConstraints(calendarConstraints())}