android 日期选择器
package com.custom.dtselector.dialog; import java.util.Calendar; import com.custom.dtselector.NumericWheelAdapter; import com.custom.dtselector.OnWheelChangedListener; import com.custom.dtselector.WheelView; import com.demo.dtselector.R; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class DateSelectorDialog extends BaseDialog { private static final int START_YEAR = 1900; private static final int END_YEAR = 2100; private int tdYear = 0; private int tdMonth = 0; private int tdDay = 0; private WheelView wlvYear = null; private WheelView wlvMonth = null; private WheelView wlvDay = null; private NumericWheelAdapter yearAdapter = null; private NumericWheelAdapter monthAdapter = null; private NumericWheelAdapter dayAdapter[] = new NumericWheelAdapter[4]; private NumericWheelAdapter curDayAdapter = null; private int sltYear = 0; private int sltMonth = 0; private int sltDay = 0; private String strSltYear = ""; private String strSltMonth = ""; private String strSltDay = ""; /**回调接口*/ private OnSelectDateEvent resultEvent = null; @Override public void init(Context context) { // TODO Auto-generated method stub this.context = context; dialog = new Dialog(this.context, R.style.CustomDialog); dialog.setContentView(R.layout.date_selector); wlvYear = (WheelView)dialog.findViewById(R.id.date_selector_year); wlvMonth = (WheelView)dialog.findViewById(R.id.date_selector_month); wlvDay = (WheelView)dialog.findViewById(R.id.date_selector_day); Button btnSelect = (Button)dialog.findViewById(R.id.btn_date_yes); btnSelect.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub selectDate(); } }); initCurDate(); initAdapters(); //对话框隐藏后,调用回调事件 dialog.setOnCancelListener(new OnCancelListener(){ @Override public void onCancel(DialogInterface arg0) { // TODO Auto-generated method stub if (resultEvent != null) { resultEvent.onSelectDateResult(sltYear, sltMonth, sltDay); } } }); } @Override public void update(Object obj) { // TODO Auto-generated method stub } /** * 设置回调事件 * @param event */ public void setOnSelectDateEvent(OnSelectDateEvent event) { this.resultEvent = event; } private void initCurDate() { Calendar instance = Calendar.getInstance(); tdYear = instance.get(Calendar.YEAR); tdMonth = instance.get(Calendar.MONTH) + 1; tdDay = instance.get(Calendar.DAY_OF_MONTH); } private int itemOfYear(int year) { return year - START_YEAR; } private int itemOfMonth(int month) { return month - 1; } private int itemOfDay(int day) { return day - 1; } /** * dapeter[0]->[1~28] dapeter[1]->[1~29] <br> * dapeter[2]->[1~30] dapeter[3]->[1~31] * @param year * @param month * @return */ private int indexAdapter(int year,int month) { int index_data[] = {31,28,31,30,31,30,31,31,30,31,30,31}; if (month == 2) { return DateTimeUtils.isLeapYear(year) ? 1 : 0; } else { if (index_data[month - 1] == 31) { return 3; } else { return 2; } } } private void initAdapters() { yearAdapter = new NumericWheelAdapter(START_YEAR,END_YEAR,"%4d"); monthAdapter = new NumericWheelAdapter(1,12,"d"); for (int i = 0; i < dayAdapter.length; i++) { dayAdapter[i] = new NumericWheelAdapter(1,28 + i,"d"); } sltYear = tdYear; sltMonth = tdMonth; sltDay = tdDay; //赋值数据 wlvYear.setVisibleItems(3); wlvYear.setCyclic(true); wlvYear.setAdapter(yearAdapter); int itemYear = itemOfYear(sltYear); wlvYear.setCurrentItem(itemYear); wlvMonth.setVisibleItems(3); wlvMonth.setCyclic(true); wlvMonth.setAdapter(monthAdapter); int itemMonth = itemOfMonth(sltMonth); wlvMonth.setCurrentItem(itemMonth); int adapterIndex = indexAdapter(sltYear,sltMonth); curDayAdapter = dayAdapter[adapterIndex]; wlvDay.setVisibleItems(3); wlvDay.setCyclic(true); wlvDay.setAdapter(curDayAdapter); int itemDay = itemOfDay(sltDay); wlvDay.setCurrentItem(itemDay); wlvYear.addChangingListener(new OnWheelChangedListener() { @Override public void onChanged(WheelView wheel, int oldValue, int newValue) { // TODO Auto-generated method stub strSltYear = yearAdapter.getItem(newValue); android.util.Log.i("",String.format("old=%d new=%d v=%s", oldValue,newValue,strSltYear)); if (strSltYear != null) { sltYear = Integer.valueOf(strSltYear); if (sltMonth == 2) { int tmpIndex = indexAdapter(sltYear,sltMonth); NumericWheelAdapter tmpAdapter = dayAdapter[tmpIndex]; if (tmpAdapter != curDayAdapter) { curDayAdapter = tmpAdapter; wlvDay.setAdapter(curDayAdapter); } } } else { sltYear = 0; } } }); wlvMonth.addChangingListener(new OnWheelChangedListener() { @Override public void onChanged(WheelView wheel, int oldValue, int newValue) { // TODO Auto-generated method stub strSltMonth = monthAdapter.getItem(newValue); android.util.Log.i("",String.format("old=%d new=%d v=%s", oldValue,newValue,strSltMonth)); if (strSltMonth != null) { sltMonth = Integer.valueOf(strSltMonth); if (sltYear != 0 && sltMonth != 0) { int tmpIndex = indexAdapter(sltYear,sltMonth); NumericWheelAdapter tmpAdapter = dayAdapter[tmpIndex]; if (tmpAdapter != curDayAdapter) { curDayAdapter = tmpAdapter; wlvDay.setAdapter(curDayAdapter); } } } else { sltMonth = 0; } } }); wlvDay.addChangingListener(new OnWheelChangedListener() { @Override public void onChanged(WheelView wheel, int oldValue, int newValue) { // TODO Auto-generated method stub strSltDay = curDayAdapter.getItem(newValue); android.util.Log.i("",String.format("old=%d new=%d v=%s", oldValue,newValue,strSltDay)); if (strSltDay != null) { sltDay = Integer.valueOf(strSltDay); } else { sltDay = 0; } } }); } private void selectDate() { hide(); } /** * 回调接口 */ public interface OnSelectDateEvent { public void onSelectDateResult(int year,int month,int day); }; }
- 1
- 粉丝: 256
- 资源: 17
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于MATLAB 使用模糊逻辑算法控制给定交叉口的红绿灯系统
- android-19安卓操作系统版本8
- springboot医院病历管理系统--论文-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- springboot在线小说阅读平台_0hxfv-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- springboot智慧生活分享平台62(源码+sql+论文)-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- springboot智能菜谱推荐系统_ct3p7--论文-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 毕业生信息招聘平台-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 大学生创新创业训练项目管理系统设计与实现-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 大健康养老公寓管理系统_to14d-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 复现一篇国内中文核心,改进的DSOGI-PLL锁相环 能够对含有电压直流分量或者是含有高次谐波 都能够锁定电压基波频率50HZ
- 大学新生报到系统的设计与实现-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 大学生社团活动平台-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 儿童性教育网站-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 点餐平台网站-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 个性化电影推荐系统-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 高校学生饮食推荐系统_02187-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip