赞
踩
学习并记录一下,使用GitHub上面的日历控件开源库CalendarView,地址:https://github.com/huanghaibin-dev/CalendarView
实现效果如下:
- dependencies {
- ......
- //日历控件
- implementation 'com.haibin:calendarview:3.6.2'
- ......
- }
MultiMonthView.java继承BaseMonthView.java查看该类,里面的不同日历子view的画笔都在里面有定义,可以根据需要进行设置,要什么view就使用什么画笔进行绘制。
比如我这里的四个画笔分别对应的含义:
mCurDayLunarTextPaint 当前日期文本颜色画笔
mSchemeTextPaint 标记的文本画笔
mOtherMonthTextPaint 其它月份日期颜色
mCurDayTextPaint 当前日期文本颜色画笔
- public class CustomMonthView extends MultiMonthView {
- private int mRadius;
-
- public CustomMonthView(Context context) {
- super(context);
- }
-
- @Override
- protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelectedPre, boolean isSelectedNext) {
- //绘制选中的背景
- int cx = x + mItemWidth / 2;
- int cy = y + mItemHeight / 3 + 10;
-
- mSelectedPaint.setAntiAlias(true);
- canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
- return true;
- }
-
- @Override
- protected void onPreviewHook() {
- mRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
- }
-
- @Override
- protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y, boolean isSelected) {
-
- }
-
- /**
- * 绘制文本
- *
- * @param canvas canvas
- * @param calendar 日历calendar
- * @param x 日历Card x起点坐标
- * @param y 日历Card y起点坐标
- * @param hasScheme 是否是标记的日期
- * @param isSelected 是否选中
- */
- @Override
- protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
- int cx = x + mItemWidth / 2;
- int top = y - mItemHeight / 8;
-
- if (hasScheme) {
- canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
- isSelected ? mSelectTextPaint
- : calendar.isCurrentDay() ? mCurDayLunarTextPaint
- : calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);
-
- } else {
- canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
- isSelected ? mSelectTextPaint
- : calendar.isCurrentDay() ? mCurDayLunarTextPaint
- : calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
- }
- if (!isSelected && calendar.isCurrentDay())
- canvas.drawText("今", cx, mTextBaseLine + top, mCurDayTextPaint);
-
- }
- }
- <com.haibin.calendarview.CalendarView
- android:id="@+id/calendarView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="10dp"
- android:paddingRight="10dp"
- app:calendar_height="40dp"
- app:current_day_text_color="#2079FF"
- app:layout_constraintStart_toStartOf="@+id/tv_years"
- app:layout_constraintTop_toBottomOf="@+id/tv_years"
- app:lunar_text_size="0dp"
- app:month_view="com.gwm.careco.mobile.car.views.CustomMonthView"
- app:month_view_scrollable="true"
- app:select_mode="single_mode"
- app:selected_text_color="#FFFFFF"
- app:selected_theme_color="#2079FF" />
可分别调用scrollToPre()、scrollToNext()方法,日历布局设置都在这个类CalendarView.java,里面会有获取当天、获取本月、设置月视图是否可滚动等基本属性和回调接口,都能在该类找到,里面也都是中文注释比较方便翻看。
- //滚动到上一个月
- mCalendarView.scrollToPre();
-
- //滚动到下一个月
- mCalendarView.scrollToNext();
只需继承CalendarView.OnMonthChangeListener的
- @Override
- public void onMonthChange(int year, int month) {
- if (mYears != null) mYears.setText(year + "年" + month + "月");
- }
注意:记得添加calendarView.setOnMonthChangeListener(this)
注意:记得添加CalendarView.setOnCalendarMultiSelectListener(this)
可以在如下方法内获取到选中的日期
- @Override
- public void onCalendarMultiSelect(com.haibin.calendarview.Calendar calendar, int curSize, int maxSize) {
- //获取选中的日期
- int day = calendar.getDay();
- int month = calendar.getMonth();
- }
onCalendarIntercept返回true就表示不可选中,反之亦然。
- @Override
- public boolean onCalendarIntercept(com.haibin.calendarview.Calendar calendar) {
- //获取日历上选中的日、月、年
- int day = calendar.getDay();
- int month = calendar.getMonth();
- int year = calendar.getYear();
- //获取当前日期的日、月、年
- int day1 = mCalendarView.getCurDay();
- int month1 = mCalendarView.getCurMonth();
- int year1 = mCalendarView.getCurYear();
- mIsClickable = year < year1 || (year == year1 && (month < month1 || (month == month1 && day < day1)));
- return mIsClickable;
- }
-
- @Override
- public void onCalendarInterceptClick(com.haibin.calendarview.Calendar calendar, boolean isClick) {
- ToastManager.getInstance().showToast((isClick ? "不能选择小于当前日期" : "无效日期"));
- }
完成。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。