Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在物料日历视图中转到所选日期?_Android_Calendarview - Fatal编程技术网

Android 如何在物料日历视图中转到所选日期?

Android 如何在物料日历视图中转到所选日期?,android,calendarview,Android,Calendarview,我正在我的项目中使用。我可以使用setSelectedDate()方法更改日期的选择。我有一个“今日选择”按钮。我的目的是通过单击此按钮将视图移动到当前日期。有方法goToNext()和goToPrevious()只更改一个月。但如果是向前或向后两个月或两个月以上,那么我应该如何将视图更改为所选日期或当前日期。我尝试使用invalidate()但什么也没发生 我以某种方式处理了这个问题,所以我想我应该与社区分享 用于跟踪月份的全局计数器变量 专用整数计数器 setOnMonthChangedL

我正在我的项目中使用。我可以使用setSelectedDate()方法更改日期的选择。我有一个“今日选择”按钮。我的目的是通过单击此按钮将视图移动到当前日期。有方法goToNext()和goToPrevious()只更改一个月。但如果是向前或向后两个月或两个月以上,那么我应该如何将视图更改为所选日期或当前日期。我尝试使用invalidate()但什么也没发生

我以某种方式处理了这个问题,所以我想我应该与社区分享

  • 用于跟踪月份的全局计数器变量

    专用整数计数器

  • setOnMonthChangedListener()以侦听月份

  • 将日期与当前日期进行比较

    final Calendar currentCal=Calendar.getInstance();
    currentCal.set(Calendar.DATE,1);
    
    calendarView.setOnMonthChangedListener(new OnMonthChangedListener() {
        @Override
        public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
            if(date.getYear()==currentCal.get(Calendar.YEAR)){
                if(date.getMonth()<currentCal.get(Calendar.MONTH)){
                    --counter;
                }else if(date.getMonth()>currentCal.get(Calendar.MONTH)){
                    ++counter;
                }
            }else if(date.getYear()<currentCal.get(Calendar.YEAR)){
                --counter;
            }else if(date.getYear()>currentCal.get(Calendar.YEAR)){
                ++counter;
            }
        }
    });
    
    final Calendar currentCal=Calendar.getInstance();
    当前校准设置(日历日期,1);
    calendarView.setOnMonthChangedListener(新的OnMonthChangedListener(){
    @凌驾
    已更改的月公共无效(MaterialCalendarView小部件,日历日日期){
    if(date.getYear()==currentCal.get(Calendar.YEAR)){
    if(date.getMonth()currentCal.get(Calendar.MONTH)){
    ++计数器;
    }
    }else if(date.getYear()currentCal.get(Calendar.YEAR)){
    ++计数器;
    }
    }
    });
    
  • 当按钮触发时,检查计数器并使用goToNext()和goToPrevious()方法将视图更改为当前日期

    int c=Math.abs(counter);
    for (int i = 0; i <c ; i++) {
        if(counter<0){
            calendarView.goToNext();
        }else if(counter>0){
            calendarView.goToPrevious();
        }
    }
    
    int c=Math.abs(计数器);
    
    对于(inti=0;i我以某种方式处理了这个问题,所以我认为我应该与社区分享它

    • 用于跟踪月份的全局计数器变量

      专用整数计数器;

    • setOnMonthChangedListener()以侦听月份

    • 将日期与当前日期进行比较

      final Calendar currentCal=Calendar.getInstance();
      currentCal.set(Calendar.DATE,1);
      
      calendarView.setOnMonthChangedListener(new OnMonthChangedListener() {
          @Override
          public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
              if(date.getYear()==currentCal.get(Calendar.YEAR)){
                  if(date.getMonth()<currentCal.get(Calendar.MONTH)){
                      --counter;
                  }else if(date.getMonth()>currentCal.get(Calendar.MONTH)){
                      ++counter;
                  }
              }else if(date.getYear()<currentCal.get(Calendar.YEAR)){
                  --counter;
              }else if(date.getYear()>currentCal.get(Calendar.YEAR)){
                  ++counter;
              }
          }
      });
      
      final Calendar currentCal=Calendar.getInstance();
      当前校准设置(日历日期,1);
      calendarView.setOnMonthChangedListener(新的OnMonthChangedListener(){
      @凌驾
      已更改的月公共无效(MaterialCalendarView小部件,日历日日期){
      if(date.getYear()==currentCal.get(Calendar.YEAR)){
      if(date.getMonth()currentCal.get(Calendar.MONTH)){
      ++计数器;
      }
      }else if(date.getYear()currentCal.get(Calendar.YEAR)){
      ++计数器;
      }
      }
      });
      
    • 当按钮触发时,检查计数器并使用goToNext()和goToPrevious()方法将视图更改为当前日期

      int c=Math.abs(counter);
      for (int i = 0; i <c ; i++) {
          if(counter<0){
              calendarView.goToNext();
          }else if(counter>0){
              calendarView.goToPrevious();
          }
      }
      
      int c=Math.abs(计数器);
      
      对于(int i=0;i对于那些在这件事上有困难的人, 将您的gradle更新为:

      compile 'com.prolificinteractive:material-calendarview:1.4.2'
      
      有一个功能:

      calendarView.setCurrentDate(CalendarDay.from(年、月、日),true);


      无需更多计算,寻呼机将指向选定的日期。很荣幸。

      对于那些在这一点上有困难的人, 将您的gradle更新为:

      compile 'com.prolificinteractive:material-calendarview:1.4.2'
      
      有一个功能:

      calendarView.setCurrentDate(CalendarDay.from(年、月、日),true);


      无需更多计算,寻呼机将指向所选日期。非常感谢。

      使用此选项后,首先查找物料日历视图:

      calendarView = (MaterialCalendarView)findViewById(R.id.calendarView);
      calendarView.setSelectionMode(MaterialCalendarView.SELECTION_MODE_MULTIPLE);
      

      使用此选项后,首先查找物料日历视图:

      calendarView = (MaterialCalendarView)findViewById(R.id.calendarView);
      calendarView.setSelectionMode(MaterialCalendarView.SELECTION_MODE_MULTIPLE);
      

      我做了一些类似于“爱国”的事情,但我能够在日历的当前状态下完成它,而不是一个听众和计数器

      private void selectDate(Date date) {
          Calendar startCalendar = new GregorianCalendar();
          startCalendar.setTime(date);
          Calendar endCalendar = calendarView.getCurrentDate().getCalendar();
      
          calendarView.setSelectedDate(date);
      
          int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
          int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
          int monthsToMove = Math.abs(diffMonth);
          for (int i = 0; i < monthsToMove; i++) {
              if (diffMonth < 0) {
                  calendarView.goToNext();
              } else if (diffMonth > 0) {
                  calendarView.goToPrevious();
              }
          }
      }
      
      private void selectDate(日期){
      日历开始日历=新的日历开始日历();
      startCalendar.setTime(日期);
      Calendar endCalendar=calendarView.getCurrentDate().getCalendar();
      calendarView.setSelectedDate(日期);
      int diffYear=endCalendar.get(Calendar.YEAR)-startCalendar.get(Calendar.YEAR);
      int diffMonth=diffYear*12+endCalendar.get(Calendar.MONTH)-startCalendar.get(Calendar.MONTH);
      int monthsToMove=Math.abs(diffMonth);
      对于(int i=0;i0){
      calendarView.goToPrevious();
      }
      }
      }
      
      我做了一些类似于@爱国的事情,但我能够在日历的当前状态下完成它,而不是使用侦听器和计数器

      private void selectDate(Date date) {
          Calendar startCalendar = new GregorianCalendar();
          startCalendar.setTime(date);
          Calendar endCalendar = calendarView.getCurrentDate().getCalendar();
      
          calendarView.setSelectedDate(date);
      
          int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
          int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
          int monthsToMove = Math.abs(diffMonth);
          for (int i = 0; i < monthsToMove; i++) {
              if (diffMonth < 0) {
                  calendarView.goToNext();
              } else if (diffMonth > 0) {
                  calendarView.goToPrevious();
              }
          }
      }
      
      private void selectDate(日期){
      日历开始日历=新的日历开始日历();
      startCalendar.setTime(日期);
      Calendar endCalendar=calendarView.getCurrentDate().getCalendar();
      calendarView.setSelectedDate(日期);
      int diffYear=endCalendar.get(Calendar.YEAR)-startCalendar.get(Calendar.YEAR);
      int diffMonth=diffYear*12+endCalendar.get(Calendar.MONTH)-startCalendar.get(Calendar.MONTH);
      int monthsToMove=Math.abs(diffMonth);
      对于(int i=0;i0){
      calendarView.goToPrevious();
      }
      }
      }
      
      所有答案都不能满足需要 解决办法是:

       val current = calendar.time
          calendarMaterial!!.selectionMode = SELECTION_MODE_RANGE
          calendarMaterial!!.setCurrentDate(CalendarDay.from(current),true)
          calendarMaterial!!.state().edit()
                      .setMinimumDate(current)
                      .commit()
      

      这将阻止选择旧日期,但当前它将其设置为不可见。我正在处理的只是将其设置为灰色 解决办法是:

       val current = calendar.time
          calendarMaterial!!.selectionMode = SELECTION_MODE_RANGE
          calendarMaterial!!.setCurrentDate(CalendarDay.from(current),true)
          calendarMaterial!!.state().edit()
                      .setMinimumDate(current)
                      .commit()
      

      这将阻止选择旧日期,但当前它处于不可见状态。我正在处理将其设置为灰色的问题。

      对此有任何修正吗?您要求什么样的修正吗?有什么修正吗?您要求什么样的修正吗?我的帖子在日历中一次选择多个日期。是的,我可以看到您的帖子,但您需要格式化您的代码我的帖子一次选择日历中的多个日期。是的,我可以看到您的帖子,但您需要格式化代码