本文实例为大家分享了Android实现自定义日期选择器的具体代码,供大家参考,具体内容如下
项目要求效果图:
要求 “6月20 星期五” 这一项作为一个整体可以滑动,”7时”、”48分”分别作为一个滑动整体。
系统自带的DatePicker、TimePicker大家都知道,只有这种效果:
百度了很多,试了NumberPicker等都不行,本来打算自己写。网友推荐了一个开源组件WheelView,下下来试了试,发现他已经定义的很完善了,在他的基础上拓展很容易。
现将基于wheelView自定义日期选择器记录如下:
一.首先要了解WheelView为我们提供了什么:
除了我写的”DateObject”与”StringWheelAdapter”,其余都是WheelView提供的,
1. WheelView.java :可滚动的组件
主要方法:
setAdapter(new StringWheelAdapter(dateList, 7)); //设置Adapter
setVisibleItems(3); //设置显示几行数据
setCyclic(true); //设置是否循环显示数据
addChangingListener(onDaysChangedListener) //设置滑动监听器
2. WheelAdapter.java : 滑动组件的适配器的接口,子类适配器用于装载数据
public interface WheelAdapter {
/**
* Gets items count
* @return the count of wheel items
*/
public int getItemsCount();
/**
* Gets a wheel item by index.
*
* @param index the item index
* @return the wheel item text or null
*/
public String getItem(int index);
/**
* Gets maximum item length. It is used to determine the wheel width.
* If -1 is returned there will be used the default wheel width.
*
* @return the maximum item length or -1
*/
public int getMaximumLength();
}
3. OnWheelChangedListener.java : 滑动监听器接口
public interface OnWheelChangedListener {
/**
* Callback method to be invoked when current item changed
* @param wheel the wheel view whose state has changed
* @param oldValue the old value of current item
* @param newValue the new value of current item
*/
void onChanged(WheelView wheel, int oldValue, int newValue);
}
4.OnWheelScrollListener.java :滚动监听器接口(暂时没用到)
5.NumericWheelAdapter.java : 当滚动内容为纯数字时调用的适配器
6.DateObject.java : 日期实体类,用于存储、获取选择的数据
package kankan.wheel.widget;
import java.util.Calendar;
public class DateObject extends Object{
private int year ;
private int month;
private int day;
private int week;
private int hour;
private int minute;
private String listItem;
/**
* 日期对象的4个参数构造器,用于设置日期
* @param year
* @param month
* @param day
* @author sxzhang
*/
public DateObject(int year2, int month2, int day2,int week2) {
super();
this.year = year2;
int maxDayOfMonth = Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
if(day2 > maxDayOfMonth){
this.month = month2 + 1;
this.day = day2 % maxDayOfMonth;
}else{
this.month = month2;
this.day = day2;
}
this.week = week2 % 7 == 0 ? 7 : week2 % 7;
if(day == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)){
this.listItem = String.format("%02d", this.month) +"月" + String.format("%02d", this.day) +
"日 "+ " 今天 ";
}else{
this.listItem = String.format("%02d", this.month) +"月" + String.format("%02d", this.day) +
"日 "+ getDayOfWeekCN(week);
}
}
/**
* 日期对象的2个参数构造器,用于设置时间
* @param hour2
* @param minute2
* @param isHourType true:传入的是hour; false: 传入的是minute
* @author sxzhang
*/
public DateObject(int hour2,int minute2,boolean isHourType) {
super();
if(isHourType == true && hour2 != -1){ //设置小时
if(hour2 > 24){
this.hour = hour2 % 24;
}else
this.hour = hour2;
this.listItem = this.hour + "时";
}else if(isHourType == false && minute2 != -1){ //设置分钟
if(minute2 > 60)
this.minute = minute2 % 60;
else
this.minute = minute2;
this.listItem = this.minute + "分";
}
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getWeek() {