一款好看的日曆控制元件

BeCocky_H發表於2017-09-22

android 一款好看的日曆控制元件

1.依賴包

 compile 'com.prolificinteractive:material-calendarview:1.4.0'
  1. 佈局中的使用:

    <com.prolificinteractive.materialcalendarview.MaterialCalendarView
           android:id="@+id/calendarView"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           app:mcv_showOtherDates="all"
           app:mcv_arrowColor="?attr/colorPrimary"
           app:mcv_leftArrowMask="@drawable/ic_navigation_arrow_back"
           app:mcv_rightArrowMask="@drawable/ic_navigation_arrow_forward"
           app:mcv_selectionColor="?attr/colorPrimary"  //選中時間的背景顏色
           app:mcv_headerTextAppearance="?android:attr/textAppearanceMedium"
           app:mcv_dateTextAppearance="@style/CustomDayTextAppearance"
           app:mcv_weekDayTextAppearance="?android:attr/textAppearanceMedium"  
           app:mcv_weekDayLabels="@array/custom_weekdays"  //顯示中文星期 
           app:mcv_monthLabels="@array/custom_months"   //顯示中文月份
           app:mcv_tileSize="36dp"
           app:mcv_firstDayOfWeek="thursday"  //顯示的第一個
           app:mcv_calendarMode="week"  //顯示的模式  周  月
           />
    
    1.自定義中文顯示年月日  星期
    <string-array name="custom_weekdays">
           <item>星期天</item>
           <item>星期一</item>
           <item>星期二</item>
           <item>星期三</item>
           <item>星期四</item>
           <item>星期五</item>
           <item>星期六</item>
       </string-array>
    
       <string-array name="custom_months">
           <item>一月</item>
           <item>二月</item>
           <item>三月</item>
           <item>四月</item>
           <item>五月</item>
           <item>六月</item>
           <item>七月</item>
           <item>八月</item>
           <item>九月</item>
           <item>十月</item>
           <item>十一月</item>
           <item>十二月</item>
       </string-array>
    
    
    
    

    3.在JAVA程式碼中的使用:

1.監聽選中的時間
  mSettimeTimeMv.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull         CalendarDay date, boolean selected) {
                CalendarDay selectedDate = mSettimeTimeMv.getSelectedDate();
                String data = dateInstance.format(selectedDate.getDate());
               //TODO

            }
        });

 2.設定時間範圍
  widget.setShowOtherDates(MaterialCalendarView.SHOW_ALL);
Calendar instance = Calendar.getInstance();
widget.setSelectedDate(instance.getTime());
Calendar instance1 = Calendar.getInstance();
instance1.set(instance1.get(Calendar.YEAR), Calendar.JANUARY, 1);
Calendar instance2 = Calendar.getInstance();
instance2.set(instance2.get(Calendar.YEAR), Calendar.DECEMBER, 31);
widget.state().edit()
     .setMinimumDate(instance1.getTime())
     .setMaximumDate(instance2.getTime())
     .commit();

簡書地址:http://www.jianshu.com/p/8396a4d39c01

相關文章