1

我想在选择日期范围后关闭 MaterialDatePicker,但必须单击“保存”按钮。选择开始和结束日期后如何自动关闭对话框?

val datePicker = createDatePicker()
datePicker.show(parentFragmentManager, DATE_PICKER_DIALOG_TAG)

private fun createDatePicker(): MaterialDatePicker<Pair<Long, Long>> {
    val constraintsBuilder = createCalendarConstraints()
    val datePicker = MaterialDatePicker.Builder.dateRangePicker()
        .setTitleText(getString(R.string.select_check_in_and_check_out_date))
        .setCalendarConstraints(constraintsBuilder.build())
        .build()
    datePicker.addOnPositiveButtonClickListener {
        binding.selectDate.setText(datePicker.headerText.toString())
    }
    return datePicker
}

private fun createCalendarConstraints(): CalendarConstraints.Builder {
    val dateValidator: DateValidator = DateValidatorPointForward.now()
    return CalendarConstraints.Builder()
        .setValidator(dateValidator)
}
4

1 回答 1

-2

您只需要datePicker.dismiss()在 onPositive 回调中添加

于 2021-06-28T09:36:09.433 回答