如何在Android Kotlin中禁用特定日期和多个日期日历?

huangapple go评论89阅读模式
英文:

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 -&gt;
               //my code
            }, year, month, day
        )
        datePickerDialog.datePicker.setMinDate(milliseconds)
        //val days = arrayOf&lt;Calendar&gt;(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"库来禁用特定日期的方法:

  1. 在你的应用的build.gradle文件中添加该库:
implementation 'com.wdullaer:materialdatetimepicker:4.2.3'
  1. 在你的代码中使用该库:
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:

  1. Add the library to your app's build.gradle:
implementation &#39;com.wdullaer:materialdatetimepicker:4.2.3&#39;
  1. 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 -&gt;
                // 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, &quot;DatePickerDialog&quot;)
    }

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.

huangapple
  • 本文由 发表于 2023年8月8日 20:28:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定