如何在日期选择器 kotlin 中设置最大日期 android

how can set max date in date picker kotlin android

如何在日期选择器 Kotlin 中设置最大日期 android

如何在最大日期中设置当前日期,以便用户无法输入未来日期

val date =
        OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
            myCalendar.set(Calendar.YEAR, year)
            myCalendar.set(Calendar.MONTH, monthOfYear)
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
            dob.set(year,monthOfYear,dayOfMonth)
            var age = today[Calendar.YEAR] - dob[Calendar.YEAR]
            if (today[Calendar.DAY_OF_YEAR] < dob[Calendar.DAY_OF_YEAR]) {
                age--
            }
            val ageInt = age
            val ageS = ageInt.toString()
            ("Age  : $ageS").also { binding.ageText.text = it }
            updateLabel()
        }
    binding.edtBirthdate.setOnClickListener {
        context?.let { it1 ->
            DatePickerDialog(
                it1, date, myCalendar[Calendar.YEAR],
                myCalendar[Calendar.MONTH],
                myCalendar[Calendar.DAY_OF_MONTH]
            )
                .show()
        }
    }

在 Kotlin 中,你可以做类似的事情

DatePickerDialog(
    it1, date, myCalendar[Calendar.YEAR],
    myCalendar[Calendar.MONTH],
    myCalendar[Calendar.DAY_OF_MONTH]
)
    .apply { datePicker.maxDate = Date().time }
    .show()