gpt4 book ai didi

android - MaterialDatePicker 仅在 2 个日期之间选择日期

转载 作者:行者123 更新时间:2023-12-02 13:10:56 30 4
gpt4 key购买 nike

如何向 MaterialDatePicker 添加约束以强制用户仅在 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());

    enter image description here

    关于android - MaterialDatePicker 仅在 2 个日期之间选择日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62469312/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com