Material DateRangePicker 启用带有单个日期的接受按钮

Material DateRangePicker enable Accept button with single date

我有一个 material daterangepicker 到 select 单个日期或范围。如果我想 select 一个日期,我必须 select 两次相同的日期才能启用“接受”按钮。 select 第一次约会时,有一些东西可以启用接受按钮吗?

我在 MaterialDatePicker.java 中看到他们使用:

pickerFragment.addOnSelectionChangedListener(
    new OnSelectionChangedListener<S>() {
      @Override
      public void onSelectionChanged(S selection) {
        updateHeader();
        confirmButton.setEnabled(dateSelector.isSelectionComplete());
      }

      @Override
      public void onIncompleteSelectionChanged() {
        confirmButton.setEnabled(false);
      }
    });

我想我需要使用这个或类似的东西,但我无法访问这个方法。

我用它来显示 DateRangePicker:

private fun showDatePicker() {
    val builder = MaterialDatePicker.Builder.dateRangePicker()
    builder.setTheme(R.style.CustomCalendarDatePickerTheme)
    builder.setCalendarConstraints(limitRange().build())

    val picker = builder.build()
    picker.addOnPositiveButtonClickListener {
        tv_date.text = formatRange(it.first!!, it.second!!)
        startDate = it.first
        endDate = it.second
    }

    picker.show(supportFragmentManager, picker.toString())
}

最后我创建了一个扩展 RangeDateSelector 并覆盖 select() 函数的 class,我第一次在开始和结束时设置日期是什么时候:

@SuppressLint("RestrictedApi")
class CustomMaterialDatePicker : RangeDateSelector() {

    private var first = true
    private var firstDate: Long? = null

    override fun select(selection: Long) {
        if (first || selection < firstDate!!) {
            super.select(selection)
            super.select(selection)
            firstDate = selection
            first = false
        } else {
            super.select(firstDate!!)
            super.select(selection)
            if (selection != firstDate) {
                first = true
            }
        }
    }
}

然后我这样创建 DateRangePicker:

@SuppressLint("RestrictedApi")
private fun showDatePicker() {
    val selector = CustomMaterialDatePicker()
    val builder = MaterialDatePicker.Builder.customDatePicker(selector)
    builder.setTheme(R.style.CustomCalendarDatePickerTheme)
    builder.setCalendarConstraints(limitRange().build())

    val picker = builder.build()
    picker.addOnPositiveButtonClickListener {
        tv_date.text = formatRange(it.first!!, it.second!!)
        startDate = it.first
        endDate = it.second
    }

    picker.show(supportFragmentManager, picker.toString())
}