在calendarview android中禁用周末

时间:2018-02-05 10:08:17

标签: android calendarview

我想在android的calendarview中禁用周末我该怎么办? 我尝试了下面的代码,

if(DateUtils.getDayFromTS(calendarView.getDate()) == Calendar.SATURDAY ||DateUtils.getDayFromTS(calendarView.getDate())  == Calendar.SUNDAY)
            calendarView.setSelected(false);

  public static int getDayFromTS(long timestamp) {
        Date date = getDateFromTimeStamp(timestamp);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);        
    }

1 个答案:

答案 0 :(得分:0)

使用setCustomizationRule

final Calendar calendar = Calendar.getInstance();
calendarView.setCustomizationRule(new Procedure<CalendarCell>() {
@Override
public void apply(CalendarCell argument) {
    if (argument instanceof CalendarDayCell) {
        calendar.setTimeInMillis(argument.getDate());
        ((CalendarDayCell) argument).setSelectable(
                !(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
                calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY));
    }
}
});