如何在Android Material Calender中禁用日期

时间:2016-06-06 04:31:32

标签: android

我使用材料CalendarView并基于array日期CalenderView突出显示日期。所以我想禁用除日期数组之外的日期。

 datePickerDialog = new Dialog(MainActivity.getInstance());
                    datePickerDialog.show();
                    datePickerDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    datePickerDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    datePickerDialog.setCancelable(true);
                    datePickerDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                    datePickerDialog.setContentView(R.layout.assignment_custom_datepicker);
                    materialCalendarView = (MaterialCalendarView) datePickerDialog.findViewById(R.id.calendarView);
                  //  materialCalendarView.setShowOtherDates(MaterialCalendarView.SHOW_ALL);
                    materialCalendarView.setFirstDayOfWeek(Calendar.SUNDAY);
                    materialCalendarView.invalidate();

                    materialCalendarView.setHeaderTextAppearance(R.style.TextAppearance_AppCompat_Medium);
                    materialCalendarView.setDateTextAppearance(R.drawable.background_solid_blue);
                    materialCalendarView.setDateTextAppearance(R.style.TextAppearance_AppCompat_Medium);
                    materialCalendarView.setWeekDayTextAppearance(R.style.TextAppearance_AppCompat_Medium);
                    materialCalendarView.setSelectionMode(MaterialCalendarView.SELECTION_MODE_SINGLE);

                  //  materialCalendarView.setWeekDayTextAppearance();
                    materialCalendarView.setDateTextAppearance(R.style.WalletFragmentDefaultButtonTextAppearance);
                    Calendar selectDayCalendar = Calendar.getInstance();
                    selectDayCalendar.set(mYear, mMonth, mDay);
                    materialCalendarView.addDecorators(
                            new OneDayDecorator()
                    );
                    if (calendarDays != null && !calendarDays.isEmpty() ) {
                        materialCalendarView.addDecorator(new EventDecorator(Color.RED, calendarDays));
                        materialCalendarView.setOnDateChangedListener(this);


                    }
                    else
                    {
                        // materialCalendarView.setOnDateChangedListener(this);
                    }
                    if (readcalendarDays != null && !readcalendarDays.isEmpty()) {
                        materialCalendarView.addDecorator(new EventDecorator(Color.GREEN, readcalendarDays));

                    }

2 个答案:

答案 0 :(得分:2)

尝试以下方法:

widgetTypes["CLASS_A"]["A3"] = "A3 Description";

ArrayList<CalendarDay> enabledDates = new ArrayList<>(); //Some for loop on dates{ enabledDates.add(new CalendarDay(date)); } materialCalendarView.addDecorator(calendarDecorator.new DayEnableDecorator(enabledDates)); 是一个单独的类:

DayEnableDecorator

这将启用您放入数组public class DayEnableDecorator implements DayViewDecorator{ private HashSet<CalendarDay> dates; public DayEnableDecorator(Collection<CalendarDay> dates) { this.dates = new HashSet<>(dates); } @Override public boolean shouldDecorate(CalendarDay day) { return dates.contains(day); } @Override public void decorate(DayViewFacade view) { view.setDaysDisabled(false); } } 的日期并禁用其余的

答案 1 :(得分:1)

因为目前,Ankur Aggarwal提供的答案无效 - 这是我的解决方案。

接受的答案将无效,因为默认情况下,MaterialCalendarView在启用状态下的所有日期。

首先,我们必须在MaterialCalendarView中停用所有日期。

public class AllDaysDisabledDecorator implements DayViewDecorator {

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return true; //decorate all days in calendar
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.setDaysDisabled(true); //disable all days
        }
   }

将此装饰器添加到MaterialCalendarView

materialCalendarView.addDecorator(new AllDaysDisabledDecorator());

之后,我们必须添加另一个装饰器,但是对于可用和启用的日期(如接受的答案):

public class AvailableDaysDecorator implements DayViewDecorator {

        private final int color;
        private final HashSet<CalendarDay> dates;

        AvailableDaysDecorator(int color, Collection<CalendarDay> dates) {
            this.color = color;
            this.dates = new HashSet<>(dates);
        }

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return dates.contains(day); //decorate only available days
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.setDaysDisabled(false); ///important to enable day
            view.addSpan(new DotSpan(5, color)); //adds small dot to bottom of day number text
        }
    }

并添加第二个装饰者:

materialCalendarView.addDecorator(new AvailableDaysDecorator(R.color.colorPrimary, days));

因此,在这个简单的操作之后,您将只获得所需的天数,并在其他日期禁用。