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

来源:互联网 发布:压缩的js还原 编辑:程序博客网 时间:2024/05/18 12:34

最近在项目中使用到了 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 了。

0 0