DatePicker控件是一个比较常用的控件了,但是在网上搜索到的信息却很少,好多属性方法都没有详细的介绍,自己根据实际项目使用过的一些方法,在网上搜到了很多开源的时间选择器,都没有达到自己想要的效果,还是用android自带的这个控件实现吧

DatePicker的选择时间的颜色是固定的,不知道怎么调。

DatePicker几个一定用到的方法:

初始化方法:

void android.widget.DatePicker.init(int year, int monthOfYear, int dayOfMonth, OnDateChangedListener onDateChangedListener)

该方法的介绍

Initialize the state. If the provided values designate an inconsistent date the values are normalized before updating the spinners.

Parameters:
year The initial year.
monthOfYear The initial month starting from zero.
dayOfMonth The initial day of the month.
onDateChangedListener How user is notified date is changed by user, can be null.

直接使用例子理解

Calender cal = Calender.getInstance(); // 得到时间,时区默认时间
datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                    cal.get(Calendar.DAY_OF_MONTH),
                    new DatePicker.OnDateChangedListener() {
                        @Override
                        public void onDateChanged(DatePicker view, int year,
                                int month, int day) {
                            cal.set(year, month + 1, day);
                            mYear = year + "";
                            mMonth = (month + 1) + "";
                            mDay = day + ""
                            date = mYear + "-" + mMonth + "-" + mDay;
                        }
                    });

在调用DatePicker后会先初始化其默认选择的时间

设置允许选择的最小时间

DatePicker.setMinDate(cal.getTimeInMillis());

设置允许选择的最大时间

DatePicker.setMaxDate(cal.getTimeInMillis());

目前就用到这几个简单的方法

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐