Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
909 views
in Technique[技术] by (71.8m points)

kotlin - How do you Disable Certain Days on a Datepicker - Android

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...