android 自定义日期选择控件,Android基于wheelView实现自定义日期选择器

本文实例为大家分享了Android实现自定义日期选择器的具体代码,供大家参考,具体内容如下

项目要求效果图:

982a9566c6692808a42c7ac83ce072a1.png

要求 “6月20 星期五” 这一项作为一个整体可以滑动,”7时”、”48分”分别作为一个滑动整体。

系统自带的DatePicker、TimePicker大家都知道,只有这种效果:

584f90dede1bc471dc8784086ad4fd5f.png

百度了很多,试了NumberPicker等都不行,本来打算自己写。网友推荐了一个开源组件WheelView,下下来试了试,发现他已经定义的很完善了,在他的基础上拓展很容易。

现将基于wheelView自定义日期选择器记录如下:

一.首先要了解WheelView为我们提供了什么:

f12e91aa85ad1afcd80e8cc7f1a41c75.png

除了我写的”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() {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值