Android android-times-square一款好用的日历控件


最近在项目中使用到了 android-times-square  日历控件,在网上搜了挺多博客结果都不是很满意,到 git 上看了源码。终于达到了自己想要的效果

以下是我使用的一些小细节,希望可以帮到第一次使用 android-times-square 的你。

1、先将 android-times-square 依赖到项目中去:

  compile 'com.squareup:android-times-square:1.6.5@aar'

2、在布局中使用 CalendarPickerView :


3、使用

public class MainActivity extends AppCompatActivity {
    
    private CalendarPickerView pickerView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        pickerView= (CalendarPickerView) findViewById(R.id.calendar);//初始化日历控件
        

        //将自定义的日期设置配置到日历中
        SampleDecorator decorator=new SampleDecorator(this);
        List<CalendarCellDecorator> d=new ArrayList<>();
        d.add(decorator);
        pickerView.setDecorators(d);

        //设置日历可显示的时间  add()第一个参数可选为 Week,Month,Year 第二个参数为第一个参数的数量
        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 2);

        Date today = new Date();
        //默认设置智能选择一个日期
//        pickerView.init(today, nextYear.getTime()).withSelectedDate(today);

        //如果想要选择多个日期,使用下面这行代码   通过inMode()可以选择三种形式的选择模式
        pickerView.init(today, nextYear.getTime()).inMode(CalendarPickerView.SelectionMode.RANGE);
        

    }


}

4、如果想要实现自定义日期的效果 需要自己定义一个类实现 CalendarCellDecorator 重写里面的方法来设置自己的日期背景 字体等效果

public class SampleDecorator implements CalendarCellDecorator {

    private Context context;

    public SampleDecorator(Context context) {
        this.context = context;
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void decorate(CalendarCellView cellView, Date date) {

        if (cellView.isSelectable()) { //先通过是否可选方法来区分时间,如果可选则再判断是否已选
            if (cellView.isSelected()) {//再通过是否已选来设置日期的背景图片
                cellView.setBackground(context.getResources().getDrawable(R.mipmap.ic_launcher_round, null));
            } else {
                cellView.setBackground(context.getResources().getDrawable(R.mipmap.time_13x, null));
            }
        }else {//如果为不可选时间则直接设置日期背景
            cellView.setBackground(context.getResources().getDrawable(R.mipmap.time_23x,null));
        }
    }
}
这里我只是简单的配置了不同状态下的日期背景。


到这里你应该就可以简单的使用 android-times-square 了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值