Android日期范围选择器允许范围内的不可用日期。如何防止这种情况发生?

huangapple go评论78阅读模式
英文:

Android date range picker allows unavailable dates in the range. How to prevent this?

问题

I have a date ranger picker that includes a validator for unavailable days. The validator works because I can see the unavailable dates greyed out. However, the picker allows those dates to be included in the range. I would have thought this to be default behavior to not allow date ranges that include unavailable dates, but that's not the case. How can I set the date picker to only allow date ranges that include only available dates?

val builder = MaterialDatePicker.Builder.dateRangePicker()
   .setTheme(R.style.customMaterialCalendarDatePicker)
   .setCalendarConstraints(
     CalendarConstraints.Builder()
       .setStart(Date().time)
       .setEnd(oneYearFrom().time)
       .setValidator(AvailabilityValidator(state.unavailableDays)).build()
)

Android日期范围选择器允许范围内的不可用日期。如何防止这种情况发生?

英文:

I have a date ranger picker that includes a validator for unavailable days. The validator works because I can see the unavailable dates greyed out. However, the picker allows those dates to be included in the range. I would have thought this to be default behavior to not allow date ranges that include unavailable dates, but that's not the case. How can I set the date picker to only allow date ranges that include only available dates?

val builder = MaterialDatePicker.Builder.dateRangePicker()
   .setTheme(R.style.customMaterialCalendarDatePicker)
   .setCalendarConstraints(
     CalendarConstraints.Builder()
       .setStart(Date().time)
       .setEnd(oneYearFrom().time)
       .setValidator(AvailabilityValidator(state.unavailableDays)).build()
)

Android日期范围选择器允许范围内的不可用日期。如何防止这种情况发生?

答案1

得分: 1

这是一个可能的解决方案,您可以使用DateValidatorPointForward和DateValidatorPointBackward来避免不应该输入的日期。此外,我已经尝试对代码进行了一些重构。

// 定义您的最小和最大日期
val minDate = Date() // 今天
val maxDate = oneYearFrom() // 从今天开始的一年后

// 为最小和最大日期创建验证器
val dateValidatorMin = DateValidatorPointForward.from(minDate.time)
val dateValidatorMax = DateValidatorPointBackward.before(maxDate.time)

// 将验证器组合成列表
val listValidators = mutableListOf<CalendarConstraints.DateValidator>()
listValidators.add(dateValidatorMin)
listValidators.add(dateValidatorMax)
listValidators.add(AvailabilityValidator(state.unavailableDays)) // 您的自定义验证器

// 组合所有验证器
val validators = CompositeDateValidator.allOf(listValidators)

val builder = MaterialDatePicker.Builder.dateRangePicker()
    .setTheme(R.style.customMaterialCalendarDatePicker)
    .setCalendarConstraints(
        CalendarConstraints.Builder()
            .setStart(minDate.time)
            .setEnd(maxDate.time)
            .setValidator(validators).build()
    )

如果您需要进一步的帮助,请随时告诉我。

英文:

This is a possible solution to your problem. You can use this DateValidatorPointForward and DateValidatorPointBackward to avoid dates that should not go in. Moreover, I have tried to refactor the code a bit.

// Define your min and max dates
val minDate = Date() // Today
val maxDate = oneYearFrom() // One year from today

// Create validators for min and max date
val dateValidatorMin = DateValidatorPointForward.from(minDate.time)
val dateValidatorMax = DateValidatorPointBackward.before(maxDate.time)

// Combine your validators into a list
val listValidators = mutableListOf&lt;CalendarConstraints.DateValidator&gt;()
listValidators.add(dateValidatorMin)
listValidators.add(dateValidatorMax)
listValidators.add(AvailabilityValidator(state.unavailableDays)) // your custom validator

// Combine all validators
val validators = CompositeDateValidator.allOf(listValidators)

val builder = MaterialDatePicker.Builder.dateRangePicker()
    .setTheme(R.style.customMaterialCalendarDatePicker)
    .setCalendarConstraints(
        CalendarConstraints.Builder()
            .setStart(minDate.time)
            .setEnd(maxDate.time)
            .setValidator(validators).build()
    )

huangapple
  • 本文由 发表于 2023年6月13日 01:25:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定