英文:
How can i disable specific and multiple date calendars in Android Kotlin
问题
我正在使用DatePickerDialog,但是没有显示setDisabledDays。在Kotlin中如何实现这个功能呢?我需要禁用多个日期,并且设置一个最大和最小日期范围。
fun datepicker(){
val year = calendarNow.get(Calendar.YEAR)
val month = calendarNow.get(Calendar.MONTH)
val day = calendarNow.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(this,
{ view, year, monthOfYear, dayOfMonth ->
//我的代码
}, year, month, day
)
datePickerDialog.datePicker.setMinDate(milliseconds)
//val days = arrayOf<Calendar>(2023-08-22, 2023-08-12, 2023-08-15) //这些是要禁用的日期
//datePickerDialog.setDisabledDays() //这一行不起作用,如何解决?
datePickerDialog.show()
}
英文:
I am Using DatePickerDialog. but not showing setDisabledDays. so how is it possible in kotlin. My required is multiple dates disable and also one max and min range of calendar
fun datepicker(){
val year = calendarNow.get(Calendar.YEAR)
val month = calendarNow.get(Calendar.MONTH)
val day = calendarNow.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(this,
{ view, year, monthOfYear, dayOfMonth ->
//my code
}, year, month, day
)
datePickerDialog.datePicker.setMinDate(milliseconds)
//val days = arrayOf<Calendar>(2023-08-22, 2023-08-12, 2023-08-15) //this is disable dates
//datePickerDialog.setDisabledDays() //this line is not working how to solved this
datePickerDialog.show()
}
答案1
得分: 1
你可以使用"MaterialDateTimePicker"库,该库提供了增强功能,包括禁用特定日期、设置最小和最大年份等等...
以下是如何使用"MaterialDateTimePicker"库来禁用特定日期的方法:
- 在你的应用的build.gradle文件中添加该库:
implementation 'com.wdullaer:materialdatetimepicker:4.2.3'
- 在你的代码中使用该库:
fun datepicker() {
val disabledDays = arrayOf(
Calendar.getInstance().apply {
set(Calendar.YEAR, 2023)
set(Calendar.MONTH, Calendar.AUGUST)
set(Calendar.DAY_OF_MONTH, 22)
},
Calendar.getInstance().apply {
set(Calendar.YEAR, 2023)
set(Calendar.MONTH, Calendar.AUGUST)
set(Calendar.DAY_OF_MONTH, 12)
},
Calendar.getInstance().apply {
set(Calendar.YEAR, 2023)
set(Calendar.MONTH, Calendar.AUGUST)
set(Calendar.DAY_OF_MONTH, 15)
}
)
val now = Calendar.getInstance()
val minYear = 2023
val maxYear = 2023
val minMonth = Calendar.JANUARY // 0-based index
val maxMonth = Calendar.DECEMBER // 0-based index
val minDay = 1
val maxDay = 25
val dpd = DatePickerDialog.newInstance(
OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// 处理选定的日期
},
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
)
dpd.setDisabledDays(disabledDays)
dpd.setMinDate(Calendar.getInstance().apply {
set(minYear, minMonth, minDay)
})
dpd.setMaxDate(Calendar.getInstance().apply {
set(maxYear, maxMonth, maxDay)
})
dpd.show(supportFragmentManager, "DatePickerDialog")
}
在这个例子中,我使用"MaterialDateTimePicker"库创建了一个日期选择对话框。我们设置了disabledDays
数组,其中包含你想要禁用的特定日期以及最小和最大年份。
库: MaterialDateTimePicker 如果你想了解更多信息。
英文:
You can use this "MaterialDateTimePicker" library, which provides enhanced features including the ability to disable specific dates and Min & Max Years and many more...
Here's how you can use the "MaterialDateTimePicker" library to disable specific dates:
- Add the library to your app's build.gradle:
implementation 'com.wdullaer:materialdatetimepicker:4.2.3'
- Use the library in your code:
fun datepicker() {
val disabledDays = arrayOf(
Calendar.getInstance().apply {
set(Calendar.YEAR, 2023)
set(Calendar.MONTH, Calendar.AUGUST)
set(Calendar.DAY_OF_MONTH, 22)
},
Calendar.getInstance().apply {
set(Calendar.YEAR, 2023)
set(Calendar.MONTH, Calendar.AUGUST)
set(Calendar.DAY_OF_MONTH, 12)
},
Calendar.getInstance().apply {
set(Calendar.YEAR, 2023)
set(Calendar.MONTH, Calendar.AUGUST)
set(Calendar.DAY_OF_MONTH, 15)
}
)
val now = Calendar.getInstance()
val minYear = 2023
val maxYear = 2023
val minMonth = Calendar.JANUARY // 0-based index
val maxMonth = Calendar.DECEMBER // 0-based index
val minDay = 1
val maxDay = 25
val dpd = DatePickerDialog.newInstance(
OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// Handle selected date
},
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
)
dpd.setDisabledDays(disabledDays)
dpd.setMinDate(Calendar.getInstance().apply {
set(minYear, minMonth, minDay)
})
dpd.setMaxDate(Calendar.getInstance().apply {
set(maxYear, maxMonth, maxDay)
})
dpd.show(supportFragmentManager, "DatePickerDialog")
}
In this example, i'm using the "MaterialDateTimePicker" library to create a date picker dialog. We've set the disabledDays
array with specific dates that you want to disable and min max years.
Library: MaterialDateTimePicker in case you want to see more about it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论