日历左右单击箭头的月份启用和禁用基于android中的某些日期

时间:2018-03-22 11:20:10

标签: android

enter image description here enter image description here

我有一个日历代码,在页面顶部我们提到了两年。 左侧上一年和右侧当前年份 因此,一旦用户打开日历,当前年份,日期和月份将在日历上显示为默认值。 对于日历,双方左右箭头都可以进入下一个月和前几个月。

例如:如果我们有2017年和2018年,根据要求,如果用户选择2017年1月,则应禁用左箭头。如果用户单击右箭头,则用户应该可以使用到2017年12月,之后,应禁用右箭头。 然后,当用户点击2018,然后移至2018年1月,则应禁用左箭头。同样,本月的右箭头应该只在当月启用。 我尝试了不同的方式,但无法在某个月内禁用点击。

//next arrow click for showing next month
nextMonth.setOnClickListener(new View.OnClickListener() {
 @Override
  public void onClick(View v) {

   if (month > 11) {//checking the month
     month = 1;
     year++;
   } else {
   month++;
   }
   setGridCellAdapterToDate(month, year);
   }
   });

//prev arrow click to show prev month
    prevMonth.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    if (month <= 1) {//checking the month
        month = 12;
        year--;
    } else {
    month--;
    }

    setGridCellAdapterToDate(month, year);
                        }
   });

任何帮助都会非常感激

2 个答案:

答案 0 :(得分:1)

使用以下代码更新calendarMy对象中的当前时间并检查当前月份和年份

Calendar calendar = Calendar.getInstance();
Calendar calendarMy = Calendar.getInstance();

nextMonth.setOnClickListener(new View.OnClickListener() {
 @Override
  public void onClick(View v) {

   if (month > 11) {//checking the month
     month = 1;
     year++;
   } else {
   month++;
   }

   //update current time
    calendarMy.set(Calendar.MONTH,month);
    calendarMy.set(Calendar.YEAR,year);

    disableButton();

   setGridCellAdapterToDate(month, year);
   }
   });

//prev arrow click to show prev month
    prevMonth.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    if (month <= 1) {//checking the month
        month = 12;
        year--;
    } else {
    month--;
    }

    //update current time
     calendarMy.set(Calendar.MONTH,month);
    calendarMy.set(Calendar.YEAR,year);

    disableButton();


    setGridCellAdapterToDate(month, year);
                        }
   });

这里我添加了两个条件来检查去年的第一个月和当前的运行月份

public void disableButton()
   {

   // condition for checking first month of last year
     if (calendar.get(Calendar.YEAR) != (calendarMy.get(Calendar.YEAR)) && calendarMy.get(Calendar.MONTH) == 0) {
            //disable left button
        }
        // condition for checking current month
        else if (calendarMy.get(Calendar.YEAR) == (calendarMy.get(Calendar.YEAR)) && calendar.get(Calendar.MONTH) == calendarMy.get(Calendar.MONTH)) {

            //disable right button
        }else{
        // enable both button
        }


   }

答案 1 :(得分:1)

Just Pass start month in " myCalendar.get(Calendar.MONTH)".Happy coding...



 DatePickerDialog dpd = new DatePickerDialog(ActivityNewGroup.this, date,
                            myCalendar.get(Calendar.YEAR),
                            myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH));
                    dpd.getDatePicker().setMinDate(System.currentTimeMillis());