在日历Android中禁用整个星期(周末除外)

时间:2019-01-31 09:13:58

标签: android datepicker calendar

我当前正在使用MaterialDateTimePicker库作为日期和时间选择器, 但是我面临的问题是我无法禁用整个星期,除非我从API获得某些天。

在库中它说您可以通过使用

来实现类似的目的
  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;
                }
                dpd.setSelectableDays(days);

但是我该如何更改以按名称禁用工作日

1 个答案:

答案 0 :(得分:0)

经过一些搜索和调整后,我设法使用以下代码进行了操作:

                Calendar day;
                List<Calendar> weekends = new ArrayList<>();
                int weeks = 5;

                for (int i = 0; i < (weeks * 7); i = i + 7) {

                    for (int d = 0; d < slots.getResponse().getDays().size(); d++) {

                        day = Calendar.getInstance();
                        switch (slots.getResponse().getDays().get(d)) {
                            case "Sunday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Monday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.MONDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Tuesday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.TUESDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Wednesday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.WEDNESDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Thursday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.THURSDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Friday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.FRIDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Saturday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;

                        }

                    }
                }
                Calendar[] SelectableDays = weekends.toArray(new Calendar[weekends.size()]);
                dpd.setSelectableDays(SelectableDays);