如何在 MaterialDatePicker 中禁用过去的日期? [英] How to disable past dates in MaterialDatePicker?

本文介绍了如何在 MaterialDatePicker 中禁用过去的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 //Material Date Picker
private val calendar = Calendar.getInstance()
private val constraintsBuilder = CalendarConstraints.Builder().setStart(calendar.timeInMillis)
private val builder = MaterialDatePicker.Builder.datePicker()
private val picker = builder
    .setSelection(calendar.timeInMillis)
    .setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR)
    .setCalendarConstraints(constraintsBuilder.build())
    .build()

我使用上面的代码打开日历 setSelection 方法选择当前日期和 setStart 只适用于当月所以我无法选择上个月的日期但我仍然可以选择当月的过去日期.

I am using above code to open calendar setSelection method select current date and setStart only works for current month So i am unable to select previous month dates but i can still select past dates of current month.

如果有人可以帮助我.请告诉我.谢谢.

If any can help me out. Please let me know. Thanks.

推荐答案

您可以构建自己的 DateValidator 或者您可以只使用 DateValidatorPointForward 由图书馆提供.

You can build your own DateValidator or you can just use the DateValidatorPointForward provided by the library.

类似于:

//Returns a DateValidator which enables days from {@code point}, in 
//UTC milliseconds, forward
CalendarConstraints.DateValidator dateValidator = DateValidatorPointForward.from(yourDate);
constraintsBuilder.setValidator(dateValidator);

这篇关于如何在 MaterialDatePicker 中禁用过去的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆