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币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- uniapp vue3 自定义下拉刷新组件pullRefresh,带释放刷新状态、更新时间、加载动画
- WINDOWS 2003邮箱服务器搭建
- AI's prompts
- 该存储库将演示如何使用 OpenVINO 运行时 API 部署官方 YOLOv7 预训练模型.zip
- 该存储库包含使用 YOLOv9 对象检测模型和 DeepSORT 算法在视频中进行对象检测和跟踪的代码 .zip
- 论文《YOLO-ReT在边缘 GPU 上实现高精度实时物体检测》的实现.zip
- 让yolov6可以更方便的改变网络结构.zip
- springboot0桂林旅游景点导游平台(代码+数据库+LW)
- mmexportf3d00a398950f9982c0f132475da3f26_1732379945062.jpeg
- mmexport1732556836794.jpg