Android自定义时间选择器,支持YMDHMS,YMD,HMS
2017-09-25 本文已影响165人
老夫掐指一算z
TimePickerView
功能介绍
1.支持YMDHMS,YMD,HMS,HM常用类型时间显示
2.支持过去时间过滤,不显示出来
3.支持设置当前默认显示时间
4.功能与UI分离,支持样式扩展
功能实现说明
1.通过initDisplayMode方法,初始化显示模式,在初始化TimePickerView对象后设置
timePickerView = new TimePickerView(MainActivity.this);
timePickerView.setMode(TimePickerView.DISPLAY_HM);
2.在弹出时间控件前,会根据设置判断,通过initPickViewDisplayData方法设置年月日时分秒的显示数据
//初始化显示数据
private void initPickViewDisplayData(){
try {
if(StringUtil.isEmpty(displayTime)){
pvw_year.setSelected(0);
pvw_month.setSelected(0);
pvw_day.setSelected(0);
pvw_hour.setSelected(0);
pvw_minute.setSelected(0);
pvw_second.setSelected(0);
select_year = pvw_year.getSelected();
select_month = pvw_month.getSelected();
select_day = pvw_day.getSelected();
select_hour = pvw_hour.getSelected();
select_minute = pvw_minute.getSelected();
select_second = pvw_second.getSelected();
}else{
Date date = DateUtil.str2Date(displayTime, mode);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
select_year = DateUtil.getYear(startTime, DateUtil.PATTERN_ALL)+"";
select_month = DecimalUtil.keep2zero_head(DateUtil.getMonth(startTime, DateUtil.PATTERN_ALL)+"");
select_day = DecimalUtil.keep2zero_head(DateUtil.getDay(startTime, DateUtil.PATTERN_ALL)+"");
select_hour = DecimalUtil.keep2zero_head(DateUtil.getHour(startTime, DateUtil.PATTERN_ALL)+"");
select_minute = DecimalUtil.keep2zero_head(DateUtil.getMinute(startTime, DateUtil.PATTERN_ALL)+"");
select_second =DecimalUtil.keep2zero_head(DateUtil.getSecond(startTime, DateUtil.PATTERN_ALL)+"");
switch (mode) {
case DISPLAY_YMDHMS://yyyy-MM-dd HH:mm:ss
select_year = calendar.get(Calendar.YEAR)+"";
select_month = DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.MONTH)+1));
select_day = DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.DATE)));
select_hour = DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.HOUR_OF_DAY)));
select_minute = DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.MINUTE)));
select_second =DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.SECOND)));
initData();
pvw_year.setSelected(select_year);
pvw_month.setSelected(select_month);
pvw_day.setSelected(select_day);
pvw_hour.setSelected(select_hour);
pvw_minute.setSelected(select_minute);
pvw_second.setSelected(select_second);
break;
case DISPLAY_YMD://yyyy-MM-dd
select_year = calendar.get(Calendar.YEAR)+"";
select_month = DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.MONTH)+1));
select_day = DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.DATE)));
initData();
pvw_year.setSelected(select_year);
pvw_month.setSelected(select_month);
pvw_day.setSelected(select_day);
break;
case DISPLAY_HMS://HH:mm:ss
select_hour = DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.HOUR_OF_DAY)));
select_minute = DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.MINUTE)));
select_second =DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.SECOND)));
initData();
pvw_hour.setSelected(select_hour);
pvw_minute.setSelected(select_minute);
pvw_second.setSelected(select_second);
break;
case DISPLAY_HM://HH:mm:ss
select_hour = DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.HOUR_OF_DAY)));
select_minute = DecimalUtil.keep2zero_head(""+(calendar.get(Calendar.MINUTE)));
initData();
pvw_hour.setSelected(select_hour);
pvw_minute.setSelected(select_minute);
break;
default:
break;
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
3.初始化时间数据的时候,通过调用initData方法初始年月日时分秒滚动组件的数据集合
//初始日
private void initDay(boolean isSlide){
int start_day = DateUtil.getDay(startTime, DateUtil.PATTERN_ALL);
int day_of_month = DateUtil.getDayOfMonth(startTime,DateUtil.PATTERN_ALL);
int start_year = DateUtil.getYear(startTime, DateUtil.PATTERN_ALL);
int start_month = DateUtil.getMonth(startTime, DateUtil.PATTERN_ALL);
String select_time = select_year + "-" + select_month;
String start_time = start_year + "-" + DecimalUtil.keep2zero_head(start_month + "");
//判断当前选择时间是否是开始时间
if (!select_time.equals(start_time)) {
start_day = 1;
}
String now_time = select_year + "-" + select_month + "-01 00:00:00";
day_of_month = DateUtil.getDayOfMonth(now_time, DateUtil.PATTERN_ALL);
day_list.clear();
for(int i=start_day;i<=day_of_month;i++){
day_list.add(DecimalUtil.keep2zero_head(i+""));
}
//滑动时执行
if(isSlide){
pvw_day.invalidate();
pvw_day.setSelected(0);
select_day = pvw_day.getSelected();
excuteAnimator(ANIMATORDELAY, pvw_day);
pvw_day.postDelayed(new Runnable() {
@Override
public void run() {
initHour(true);
}
}, CHANGEDELAY);
}
}
以下是效果图
YMDHMS
YMD
HMS
HM
YMD 过滤过去时间
源码下载
参考博客
http://blog.csdn.net/zhongkejingwang/article/details/38513301