英文:
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()
)
英文:
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()
)
答案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<CalendarConstraints.DateValidator>()
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()
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论