如何禁用 Android MaterialDatePicker 中的未来日期?
How to disable future dates in Android MaterialDatePicker?
我正在尝试使用禁用未来日期的 MaterialDatePicker 创建一个简单的 DatePicker。我尝试了其他类似 Whosebug 问题的几个答案,但没有用。到目前为止,这是我的代码:-
val datePicker = MaterialDatePicker.Builder.datePicker().apply {
setTitleText(getString(R.string.label_select_dob))
setCalendarConstraints(limitRange().build())
}.build()
private fun limitRange(): CalendarConstraints.Builder {
val calendarStart: Calendar = Calendar.getInstance()
val calendarEnd: Calendar = Calendar.getInstance()
val year = 1900
val startMonth = 1
val startDate = 1
calendarStart.set(year, startMonth, startDate)
calendarEnd.set(2021, 3, 27)
val minDate: Long = calendarStart.timeInMillis
val maxDate: Long = calendarEnd.timeInMillis
val constraintsBuilder = CalendarConstraints.Builder()
val validators: ArrayList<CalendarConstraints.DateValidator> = ArrayList()
validators.add(DateValidatorPointForward.from(minDate))
validators.add(DateValidatorPointBackward.before(maxDate))
constraintsBuilder.setValidator(CompositeDateValidator.allOf(validators))
return constraintsBuilder
}
要禁用未来日期,只需使用图书馆提供的 DateValidatorPointBackward
。
今天:
val constraintsBuilder =
CalendarConstraints.Builder()
.setValidator(
DateValidatorPointBackward.now())
如果您需要自定义日期(4 月 1 日之后的所有日期):
val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
calendar[Calendar.DAY_OF_MONTH] = 1
calendar[Calendar.MONTH] = Calendar.APRIL
val april = calendar.timeInMillis
val constraintsBuilder =
CalendarConstraints.Builder()
.setValidator(
DateValidatorPointBackward.before(april))
val constraintsBuilder =
CalendarConstraints.Builder()
.setValidator(DateValidatorPointBackward.now())
val dataPicker = MaterialDatePicker.Builder.datePicker().apply {
setTitleText("Select Previous Dates")
setCalendarConstraints(constraintsBuilder.build())
}.build()
首先创建一个约束生成器
val constraintsBuilder = CalendarConstraints.Builder().setValidator(DateValidatorPointBackward.now())
第二次将其传递给 setCalendarConstraints 方法
val dialog1 = MaterialDatePicker.Builder.datePicker().setCalendarConstraints(constraintsBuilder.build())
赋值给一个变量并显示
val picker = dialog1.build()
picker.show(adapter.getFragmentManager(), picker.toString())
有关Material日期选择器库的更多信息,您可以访问以下link:
我正在尝试使用禁用未来日期的 MaterialDatePicker 创建一个简单的 DatePicker。我尝试了其他类似 Whosebug 问题的几个答案,但没有用。到目前为止,这是我的代码:-
val datePicker = MaterialDatePicker.Builder.datePicker().apply {
setTitleText(getString(R.string.label_select_dob))
setCalendarConstraints(limitRange().build())
}.build()
private fun limitRange(): CalendarConstraints.Builder {
val calendarStart: Calendar = Calendar.getInstance()
val calendarEnd: Calendar = Calendar.getInstance()
val year = 1900
val startMonth = 1
val startDate = 1
calendarStart.set(year, startMonth, startDate)
calendarEnd.set(2021, 3, 27)
val minDate: Long = calendarStart.timeInMillis
val maxDate: Long = calendarEnd.timeInMillis
val constraintsBuilder = CalendarConstraints.Builder()
val validators: ArrayList<CalendarConstraints.DateValidator> = ArrayList()
validators.add(DateValidatorPointForward.from(minDate))
validators.add(DateValidatorPointBackward.before(maxDate))
constraintsBuilder.setValidator(CompositeDateValidator.allOf(validators))
return constraintsBuilder
}
要禁用未来日期,只需使用图书馆提供的 DateValidatorPointBackward
。
今天:
val constraintsBuilder =
CalendarConstraints.Builder()
.setValidator(
DateValidatorPointBackward.now())
如果您需要自定义日期(4 月 1 日之后的所有日期):
val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
calendar[Calendar.DAY_OF_MONTH] = 1
calendar[Calendar.MONTH] = Calendar.APRIL
val april = calendar.timeInMillis
val constraintsBuilder =
CalendarConstraints.Builder()
.setValidator(
DateValidatorPointBackward.before(april))
val constraintsBuilder =
CalendarConstraints.Builder()
.setValidator(DateValidatorPointBackward.now())
val dataPicker = MaterialDatePicker.Builder.datePicker().apply {
setTitleText("Select Previous Dates")
setCalendarConstraints(constraintsBuilder.build())
}.build()
首先创建一个约束生成器
val constraintsBuilder = CalendarConstraints.Builder().setValidator(DateValidatorPointBackward.now())
第二次将其传递给 setCalendarConstraints 方法
val dialog1 = MaterialDatePicker.Builder.datePicker().setCalendarConstraints(constraintsBuilder.build())
赋值给一个变量并显示
val picker = dialog1.build()
picker.show(adapter.getFragmentManager(), picker.toString())
有关Material日期选择器库的更多信息,您可以访问以下link: