TimePickerView
功能介紹
1.支持YMDHMS,YMD,HMS,HM常用類(lèi)型時(shí)間顯示
2.支持過(guò)去時(shí)間過(guò)濾乘粒,不顯示出來(lái)
3.支持設(shè)置當(dāng)前默認(rèn)顯示時(shí)間
4.功能與UI分離豌注,支持樣式擴(kuò)展
功能實(shí)現(xiàn)說(shuō)明
1.通過(guò)initDisplayMode方法,初始化顯示模式灯萍,在初始化TimePickerView對(duì)象后設(shè)置
timePickerView = new TimePickerView(MainActivity.this);
timePickerView.setMode(TimePickerView.DISPLAY_HM);
2.在彈出時(shí)間控件前轧铁,會(huì)根據(jù)設(shè)置判斷,通過(guò)initPickViewDisplayData方法設(shè)置年月日時(shí)分秒的顯示數(shù)據(jù)
//初始化顯示數(shù)據(jù)
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.初始化時(shí)間數(shù)據(jù)的時(shí)候竟稳,通過(guò)調(diào)用initData方法初始年月日時(shí)分秒滾動(dòng)組件的數(shù)據(jù)集合
//初始日
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 + "");
//判斷當(dāng)前選擇時(shí)間是否是開(kāi)始時(shí)間
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+""));
}
//滑動(dòng)時(shí)執(zhí)行
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 過(guò)濾過(guò)去時(shí)間
源碼下載
參考博客
http://blog.csdn.net/zhongkejingwang/article/details/38513301