时间选择器:MaterialCalendarView控件使用

实现如下效果:
在这里插入图片描述

使用CalendarView或DatePicker也能有类似效果,未使用该两种组件原因如下:

1. DatePicker不能调节日期的字体大小、
2. CalendarView可以调节时间字体大小,但是顶部年月字体大小未找到如何调整。

因此选择了使用开源的MaterialCalendarView。

使用步骤如下:

引入相关依赖:

implementation 'com.prolificinteractive:material-calendarview:1.4.3'

布局控件使用:(文末有组件部分属性介绍)

<com.prolificinteractive.materialcalendarview.MaterialCalendarView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/materialCalendarView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/CustomTextAppearance"
    app:mcv_showOtherDates="all"
    app:mcv_selectionColor="#0080FF"
    app:mcv_arrowColor="@color/white"
    app:mcv_dateTextAppearance="@dimen/text_size"
    app:mcv_weekDayTextAppearance="@dimen/text_size"
    app:mcv_headerTextAppearance="@dimen/text_size"
    />
    
    mcv_dateTextAppearance、mcv_weekDayTextAppearance、mcv_headerTextAppearance
    以上这三个属性调节不同区域字体大小貌似无效,具体字体大小在自定义样式中设置即可,但是不能去掉这三个属性否则有可能导致设置字体大小无效

自定义主题样式:

<style name="CustomTextAppearance" parent="@style/LunchTheme">
    <item name="android:textColor">@drawable/my_date_text_color</item>
    <item name="android:textSize">@dimen/text_30sp</item>
</style>

颜色样式文件:my_date_text_color.xml

颜色样式需根据实际进行修改

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:enterFadeDuration="@android:integer/config_shortAnimTime"
    android:exitFadeDuration="@android:integer/config_shortAnimTime">
    <item
        android:state_checked="true"
        android:color="@android:color/white"
        />
    <item
        android:state_pressed="true"
        android:color="@android:color/white"
        />
    <item
        android:state_enabled="false"
        android:color="#808080"  //设置不可选之后的日期颜色
        />

    <item android:color="@android:color/white" />

</selector>

activity中代码:(需要结合实际:在自定义弹窗或者fragment中使用)

MaterialCalendarView materialCalendarView = findViewById(R.id.materialCalendarView);
materialCalendarView.setSelectedDate(new Date());//设置最默认选中当前日期
materialCalendarView.state().edit()
                .setMaximumDate(new Date())
                .commit();//设置最大日期之后不可选,其他属性可以看文章末尾处
materialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull @NotNull MaterialCalendarView widget, @NonNull @NotNull CalendarDay date, boolean selected) {
                Toast.makeText(MainActivity.this,"日期是:"+date.getYear()+"年"+(date.getMonth()+1)+"月"+date.getDay()+"日",Toast.LENGTH_SHORT).show();
            }
        });

附MaterialCalendarView部分属性介绍:

以下资料来源:帝乙岩 :https://blog.csdn.net/emperor_rock/article/details/51604215?readlog

    app:mcv_allowClickDaysOutsideCurrentMonth="true"允许按当前月份的天数
    app:mcv_showOtherDates="all"                    本月外其它的日期可以被选中
    app:mcv_arrowColor=""                           箭头颜色
    app:mcv_calendarMode=""                         日历模式
    app:mcv_dateTextAppearance=""                   日期文本外观
    app:mcv_firstDayOfWeek=""                       星期一
    app:mcv_headerTextAppearance=""                 标题文本外观
    app:mcv_leftArrowMask=""                        左箭头面具
    app:mcv_monthLabels=""                          月标签
    app:mcv_rightArrowMask=""                       右箭头面具
    app:mcv_selectionColor=""                       选择颜色
    app:mcv_weekDayLabels=""                        周标签
    app:mcv_weekDayTextAppearance=""            周日期的文本外观

在源码里面有一个dynamic setters test模块,对它进行一下说明(献给英语不好的各位):

1.other dates其它的日期
    other months 月(显示前一个后一个月的日期)
    out of range超出范围
    decorated disabled 装饰禁用
    select days outside  month选择天超出月范围的(多出的日期可选择)
2.month mode 月模式显示
3.week mode 周模式显示
4.selection mode 选择模式
    no selsction 不可以选择
    single date 选择一个
    multiple dates 多个日期
5.toggle topbar 切换topbar(不显示年月)
6.previous以前的一个月
7.next 下一个月
8.set min date 设置最小日期之前不可选
9.set max date 设置最大日期之后不可选
10.set selected date设置默认选的一个日期
11.random colors随机颜色(选中的颜色)
12.set width and height 设置每个数据的宽高
13.clear selection 清空选择
14.paging enabled分页功能(否则只能选中一个月的日期)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值