重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
怎么在Android中使用wheelView实现自定义日期选择器?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
10年的延安网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整延安建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“延安网站设计”,“延安网站推广”以来,每个客户项目都认真落实执行。
一.首先要了解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 month3, int day2,int week2) { super(); this.year = year2; int maxDayOfMonth = Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH); if(day2 > maxDayOfMonth){ this.month = month3 + 1; this.day = day2 % maxDayOfMonth; }else{ this.month = month3; 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() { return week; } public void setWeek(int week) { this.week = week; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public String getListItem() { return listItem; } public void setListItem(String listItem) { this.listItem = listItem; } /** * 根据day_of_week得到汉字星期 * @return */ public static String getDayOfWeekCN(int day_of_week){ String result = null; switch(day_of_week){ case 1: result = "星期日"; break; case 2: result = "星期一"; break; case 3: result = "星期二"; break; case 4: result = "星期三"; break; case 5: result = "星期四"; break; case 6: result = "星期五"; break; case 7: result = "星期六"; break; default: break; } return result; } }
7.StringWheelAdapter.java :一会儿将定义的滚动内容为字符串的适配器,当内容为字符串时我们就可以随意拓展滑动部分的内容
package kankan.wheel.widget; import java.util.ArrayList; /** * The simple String Array wheel adapter * */ public class StringWheelAdapter implements WheelAdapter { /** The default items length */ public static final int DEFAULT_LENGTH = -1; // items private ArrayListlist; // length private int length; /** * Constructor * @param items the items * @param length the max items length */ public StringWheelAdapter(ArrayList list, int length) { this.list = list; this.length = length; } @Override public String getItem(int index) { if (index >= 0 && index < list.size()) { return list.get(index).getListItem(); } return null; } @Override public int getItemsCount() { return list.size(); } @Override public int getMaximumLength() { return length; } }
二.了解以后就可以使用他定义我们需要的了。
1.首先要做的是这个效果的部分:
我们将其命名为DatePicker:
package com.sxkeji.timeswitch.widget; import java.util.ArrayList; import java.util.Calendar; import kankan.wheel.widget.DateObject; import kankan.wheel.widget.OnWheelChangedListener; import kankan.wheel.widget.StringWheelAdapter; import kankan.wheel.widget.WheelView; import android.content.Context; import android.util.AttributeSet; import android.widget.LinearLayout; /** * 自定义的日期选择器 * @author sxzhang * */ public class DatePicker extends LinearLayout { private Calendar calendar = Calendar.getInstance(); private WheelView newDays; private ArrayListdateList ; private OnChangeListener onChangeListener; //onChangeListener private final int MARGIN_RIGHT = 20; private DateObject dateObject; //日期数据对象 //Constructors public DatePicker(Context context) { super(context); init(context); } public DatePicker(Context context, AttributeSet attrs) { super(context, attrs); init(context); } /** * 初始化 * @param context */ private void init(Context context){ int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); int week = calendar.get(Calendar.DAY_OF_WEEK); dateList = new ArrayList (); for (int i = 0; i < 7; i++) { dateObject = new DateObject(year, month, day+i, week+i); dateList.add(dateObject); } newDays = new WheelView(context); LayoutParams newDays_param = new LayoutParams(300,LayoutParams.WRAP_CONTENT); newDays_param.setMargins(0, 0, MARGIN_RIGHT, 0); newDays.setLayoutParams(newDays_param); newDays.setAdapter(new StringWheelAdapter(dateList, 7)); newDays.setVisibleItems(3); newDays.setCyclic(true); newDays.addChangingListener(onDaysChangedListener); addView(newDays); } /** * 滑动改变监听器 */ private OnWheelChangedListener onDaysChangedListener = new OnWheelChangedListener(){ @Override public void onChanged(WheelView mins, int oldValue, int newValue) { calendar.set(Calendar.DAY_OF_MONTH, newValue + 1); change(); } }; /** * 滑动改变监听器回调的接口 */ public interface OnChangeListener { void onChange(int year, int month, int day, int day_of_week); } /** * 设置滑动改变监听器 * @param onChangeListener */ public void setOnChangeListener(OnChangeListener onChangeListener){ this.onChangeListener = onChangeListener; } /** * 滑动最终调用的方法 */ private void change(){ if(onChangeListener!=null){ onChangeListener.onChange( dateList.get(newDays.getCurrentItem()).getYear(), dateList.get(newDays.getCurrentItem()).getMonth(), dateList.get(newDays.getCurrentItem()).getDay(), dateList.get(newDays.getCurrentItem()).getWeek()); } } /** * 根据day_of_week得到汉字星期 * @return */ public static String getDayOfWeekCN(int day_of_week){ String result = null; switch(day_of_week){ case 1: result = "星期日"; break; case 2: result = "星期一"; break; case 3: result = "星期二"; break; case 4: result = "星期三"; break; case 5: result = "星期四"; break; case 6: result = "星期五"; break; case 7: result = "星期六"; break; default: break; } return result; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
2.然后要做的是这个效果的部分
我们命名为TimePicker:
package com.sxkeji.timeswitch.widget; import java.util.ArrayList; import java.util.Calendar; import kankan.wheel.widget.DateObject; import kankan.wheel.widget.OnWheelChangedListener; import kankan.wheel.widget.StringWheelAdapter; import kankan.wheel.widget.WheelView; import android.content.Context; import android.util.AttributeSet; import android.widget.LinearLayout; /** * 自定义的时间选择器 * @author sxzhang * */ public class TimePicker extends LinearLayout{ private Calendar calendar = Calendar.getInstance(); private WheelView hours, mins; //Wheel picker private OnChangeListener onChangeListener; //onChangeListener private final int MARGIN_RIGHT = 15; //调整文字右端距离 private ArrayListhourList,minuteList; private DateObject dateObject; //时间数据对象 //Constructors public TimePicker(Context context) { super(context); init(context); } public TimePicker(Context context, AttributeSet attrs) { super(context, attrs); init(context); } /** * 初始化 * @param context */ private void init(Context context){ int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); hourList = new ArrayList (); minuteList = new ArrayList (); for (int i = 0; i < 24; i++) { dateObject = new DateObject(hour+i,-1,true); hourList.add(dateObject); } for (int j = 0; j < 60; j++) { dateObject = new DateObject(-1,minute+j,false); minuteList.add(dateObject); } //小时选择器 hours = new WheelView(context); LayoutParams lparams_hours = new LayoutParams(80,LayoutParams.WRAP_CONTENT); lparams_hours.setMargins(0, 0, MARGIN_RIGHT, 0); hours.setLayoutParams(lparams_hours); hours.setAdapter(new StringWheelAdapter(hourList, 24)); hours.setVisibleItems(3); hours.setCyclic(true); hours.addChangingListener(onHoursChangedListener); addView(hours); //分钟选择器 mins = new WheelView(context); mins.setLayoutParams(new LayoutParams(80,LayoutParams.WRAP_CONTENT)); mins.setAdapter(new StringWheelAdapter(minuteList,60)); mins.setVisibleItems(3); mins.setCyclic(true); mins.addChangingListener(onMinsChangedListener); addView(mins); } //listeners private OnWheelChangedListener onHoursChangedListener = new OnWheelChangedListener(){ @Override public void onChanged(WheelView hours, int oldValue, int newValue) { calendar.set(Calendar.HOUR_OF_DAY, newValue); change(); } }; private OnWheelChangedListener onMinsChangedListener = new OnWheelChangedListener(){ @Override public void onChanged(WheelView mins, int oldValue, int newValue) { calendar.set(Calendar.MINUTE, newValue); change(); } }; /** * 滑动改变监听器回调的接口 */ public interface OnChangeListener { void onChange(int hour, int munite); } /** * 设置滑动改变监听器 * @param onChangeListener */ public void setOnChangeListener(OnChangeListener onChangeListener){ this.onChangeListener = onChangeListener; } /** * 滑动最终调用的方法 */ private void change(){ if(onChangeListener!=null){ onChangeListener.onChange(getHourOfDay(), getMinute()); } } /** * 获取小时 * @return */ public int getHourOfDay(){ return hourList.get(hours.getCurrentItem()).getHour(); } /** * 获取分钟 * @return */ public int getMinute(){ return minuteList.get(mins.getCurrentItem()).getMinute(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
3.最后就可以直接使用了,我这里主界面是一个button,点击后弹出popupWindow显示日期选择器。布局文件及主Activity如下:
popupWindow布局文件:
主界面布局文件:
Activity代码:
package com.sxkeji.timeswitch.activity; import java.util.Calendar; import org.unism.wang.R; import com.sxkeji.timeswitch.widget.DatePicker; import com.sxkeji.timeswitch.widget.TimePicker; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; /** * 主页面 * @author sxzhang */ public class MyPickerActivity extends Activity { private Calendar calendar; private DatePicker dp_test; private TimePicker tp_test; private TextView tv_ok,tv_cancel; //确定、取消button private Button btn_naozhong; private PopupWindow pw; private String selectDate,selectTime; //选择时间与当前时间,用于判断用户选择的是否是以前的时间 private int currentHour,currentMinute,currentDay,selectHour,selectMinute,selectDay; //整体布局 private RelativeLayout Rl_all; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Rl_all = (RelativeLayout) findViewById(R.id.Rl_all); btn_naozhong = (Button) findViewById(R.id.btn_naozhong); calendar = Calendar.getInstance(); btn_naozhong.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { View view = View.inflate(MyPickerActivity.this, R.layout.dialog_select_time, null); selectDate = calendar.get(Calendar.YEAR) + "年" + calendar.get(Calendar.MONTH) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日" + DatePicker.getDayOfWeekCN(calendar.get(Calendar.DAY_OF_WEEK)); //选择时间与当前时间的初始化,用于判断用户选择的是否是以前的时间,如果是,弹出toss提示不能选择过去的时间 selectDay = currentDay = calendar.get(Calendar.DAY_OF_MONTH); selectMinute = currentMinute = calendar.get(Calendar.MINUTE); selectHour = currentHour = calendar.get(Calendar.HOUR_OF_DAY); selectTime = currentHour + "点" + ((currentMinute < 10)?("0"+currentMinute):currentMinute) + "分"; dp_test = (DatePicker)view.findViewById(R.id.dp_test); tp_test = (TimePicker)view.findViewById(R.id.tp_test); tv_ok = (TextView) view.findViewById(R.id.tv_ok); tv_cancel = (TextView) view.findViewById(R.id.tv_cancel); //设置滑动改变监听器 dp_test.setOnChangeListener(dp_onchanghelistener); tp_test.setOnChangeListener(tp_onchanghelistener); pw = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true); // //设置这2个使得点击pop以外区域可以去除pop // pw.setOutsideTouchable(true); // pw.setBackgroundDrawable(new BitmapDrawable()); //出现在布局底端 pw.showAtLocation(Rl_all, 0, 0, Gravity.END); //点击确定 tv_ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if(selectDay == currentDay ){ //在当前日期情况下可能出现选中过去时间的情况 if(selectHour < currentHour){ Toast.makeText(getApplicationContext(), "不能选择过去的时间\n 请重新选择", 0).show(); }else if( (selectHour == currentHour) && (selectMinute < currentMinute) ){ Toast.makeText(getApplicationContext(), "不能选择过去的时间\n 请重新选择", 0).show(); }else{ Toast.makeText(getApplicationContext(), selectDate+selectTime, 0).show(); pw.dismiss(); } }else{ Toast.makeText(getApplicationContext(), selectDate+selectTime, 0).show(); pw.dismiss(); } } }); //点击取消 tv_cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { pw.dismiss(); } }); } }); } //listeners DatePicker.OnChangeListener dp_onchanghelistener = new DatePicker.OnChangeListener() { @Override public void onChange(int year, int month, int day, int day_of_week) { selectDay = day; selectDate = year + "年" + month + "月" + day + "日" + DatePicker.getDayOfWeekCN(day_of_week); } }; TimePicker.OnChangeListener tp_onchanghelistener = new TimePicker.OnChangeListener() { @Override public void onChange(int hour, int minute) { selectTime = hour + "点" + ((minute < 10)?("0"+minute):minute) + "分"; selectHour = hour; selectMinute = minute; } }; }
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
关于怎么在Android中使用wheelView实现自定义日期选择器问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。