如何在Datepicker上禁用特定日期-Android [英] How do you Disable Certain Days on a Datepicker - Android

查看:180
本文介绍了如何在Datepicker上禁用特定日期-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现日期选择器,但是我需要禁用某些日子.例如,我不希望用户选择星期五和星期六.我在其他帖子中读到我需要使用此,但我仍然不需要了解它的工作原理以及如何在Kotlin中进行操作.有人帮我.

I'm trying to implement a datepicker but I need to disable some days. For example, I do not want the user to pick Friday and Saturday. I read in other posts that I need to use this library, but I still do not know how it works and how to do it in Kotlin. Someone help me plz.

推荐答案

在库文档中,提到了如何使用选择有效日期以及如何禁用某些日期.出于这两个目的,您必须将天数组传递给相应的方法.
例如,如果只想选择某些天,则必须将天数组传递给方法.要创建天数组列表,请使用以下代码

In the library documentation there is mentioned that how to use select able days and how to disable certain days. For both purpose you have to pass the array of days to the respective methods.
For example if you want to select certain days only you have to pass the array of days to the method. To create the days arraylist use following code

Calendar[] days = new Calendar[13];
for (int i = -6; i < 7; i++) {
    Calendar day = Calendar.getInstance();
    day.add(Calendar.DAY_OF_MONTH, i * 2);
    days[i + 6] = day;
}

现在,在初始化datpicker对话框之后,调用该方法来选择天数或禁用天数.

Now after initializing the datpicker dialog call the method to select the days or disable days.

DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(MainActivity.this,
                    cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH),
                    cal.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setDisabledDays(days)

这篇关于如何在Datepicker上禁用特定日期-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆