在android中禁用日历视图中的每个星期日

时间:2018-04-03 17:14:36

标签: java android android-calendar

如何在以下代码中仅禁用星期日? 我们找不到任何解决方案来禁用一个月的星期日

MainActivity.java:

CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
Calendar calendar = Calendar.getInstance();
calendarView.setMinDate(calendar.getTimeInMillis());
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() 
{

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        Toast.makeText(getApplicationContext(), "" + dayOfMonth, 0).show();// TODO Auto-generated method stub`enter code here`

    }
});

2 个答案:

答案 0 :(得分:1)

我在项目中使用了这段代码。看看它是否产生了理想的结果 -

//Global Variables
private Calendar lastSelectedCalendar = null;
private CalendarView calendarView;
//

calendarView = (CalendarView) findViewById(R.id.calendarView);
lastSelectedCalendar = Calendar.getInstance();
calendarView.setMinDate(lastSelectedCalendar.getTimeInMillis() - 1000);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        Calendar checkCalendar = Calendar.getInstance();
        checkCalendar.set(year, month, dayOfMonth);
        if(checkCalendar.equals(lastSelectedCalendar))
            return;
        if(checkCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
            calendarView.setDate(lastSelectedCalendar.getTimeInMillis());
        else
            lastSelectedCalendar = checkCalendar;
    }
});

答案 1 :(得分:0)

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

for (int i = 0; i < (weeks * 7) ; i = i + 7) {
    sunday = Calendar.getInstance();
    sunday.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - sunday.get(Calendar.DAY_OF_WEEK) + 7 + i));
    // saturday = Calendar.getInstance();
    // saturday.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - saturday.get(Calendar.DAY_OF_WEEK) + i));
    // weekends.add(saturday);
    weekends.add(sunday);
}
Calendar[] disabledDays = weekends.toArray(new Calendar[weekends.size()]);
dpd.setDisabledDays(disabledDays);
  

此代码禁用接下来的5个星期日,如果您希望长时间执行此操作,只需要修改一周。如果您也希望禁用星期六,请取消注释这些行。

     

如果您想在之前的5个星期日进行,只需将for循环修改为:

 for (int i = 0; i < (weeks * 7); i = i + 7) {
for(int j =0; j > (weeks*7) ; j = j - 7);
sunday = Calendar.getInstance();
sunday.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - sunday.get(Calendar.DAY_OF_WEEK) + 7 + i));
    // saturday = Calendar.getInstance();
    // saturday.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - saturday.get(Calendar.DAY_OF_WEEK) + i));
    // weekends.add(saturday);
    weekends.add(sunday);
}