How to disable past dates in MaterialDatePicker?

后端 未结 2 1110
野的像风
野的像风 2021-01-23 09:36
 //Material Date Picker
private val calendar = Calendar.getInstance()
private val constraintsBuilder = CalendarConstraints.Builder().setStart(calendar.timeInMillis)
priv         


        
相关标签:
2条回答
  • 2021-01-23 09:56

    You need to create your own DateValidator for this. A date being enabled/disabled depends on whether DateValidator.isValid() returns true or false

    @Parcelize
    class FutureDateValidator : CalendarConstraints.DateValidator {
        override fun isValid(date: Long): Boolean {
            return date > System.currentTimeMillis()
        }
    }
    

    And then use this validator as:

    CalendarConstraints.Builder()
        .setValidator(FutureDateValidator())
        .build()
    
    0 讨论(0)
  • 2021-01-23 10:02

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

    Something like:

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

    0 讨论(0)
提交回复
热议问题