在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → 自定义dialog实现日期选择器

自定义dialog实现日期选择器

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:5.86M
  • 下载次数:22
  • 浏览次数:294
  • 发布时间:2017-03-14
  • 实例类别:Android平台开发
  • 发 布 人:少羽
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 日期 Dialog 选择器

实例介绍

【实例简介】
【实例截图】
【核心代码】
public class DateDialog extends Dialog implements WheelView.OnSelectListener{ private TextView tv_date_cancel, tv_date_sure;  private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器  private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器  Context mContext;  TextView tv_title;  String title,date;  CallBackListener mCallBackListener;   /**  * 获取选择的年  *  * @return  */  public String getYear() {
        Log.d("TAG", mWheelYear.getSelectedText());  return mWheelYear.getSelectedText();  } /**  * 获取选择的月  *  * @return  */  public String getMonth() {
        Log.d("TAG", mWheelMonth.getSelectedText());  return mWheelMonth.getSelectedText();  } /**  * 获取选择的日  *  * @return  */  public String getDay() {
        Log.d("TAG", mWheelDay.getSelectedText());  return mWheelDay.getSelectedText();  } public String getDate() {
        Log.d("TAG", mWheelYear.getSelectedText()   "-"   mWheelMonth.getSelectedText()   "-"   mWheelDay.getSelectedText());  date = mWheelYear.getSelectedText()   "-"   mWheelMonth.getSelectedText()   "-"   mWheelDay.getSelectedText();  return date;  } private WheelView mWheelYear;  private WheelView mWheelMonth;  private WheelView mWheelDay;     public DateDialog(Context context, String str) { super(context);  mContext = context;  title = str;  } public DateDialog(Context context, int themeResId) { super(context, R.style.MyDialog);  } /**  * 设置取消按钮的显示内容和监听  *  * @param onNoOnclickListener  */  public void setNoOnclickListener(onNoOnclickListener onNoOnclickListener) { this.noOnclickListener = onNoOnclickListener;  } /**  * 设置确定按钮的显示内容和监听  *  * @param onYesOnclickListener  */  public void setYesOnclickListener(onYesOnclickListener onYesOnclickListener) { this.yesOnclickListener = onYesOnclickListener;  } @Override  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);  setContentView(R.layout.datepicker_layout);  //按空白处不能取消动画  setCanceledOnTouchOutside(false);  //初始化界面控件  setupViews();  //操作事件  initEvent();  } private void initEvent() { tv_date_cancel.setOnClickListener(new View.OnClickListener() { @Override  public void onClick(View v) { if (noOnclickListener != null) { noOnclickListener.onNoClick();  }
            }
        });  tv_date_sure.setOnClickListener(new View.OnClickListener() { @Override  public void onClick(View v) { if (yesOnclickListener != null) { yesOnclickListener.onYesClick();  mCallBackListener.getDate(date);  }
            }
        });  } private void setupViews() { tv_date_cancel = (TextView) findViewById(R.id.tv_date_cancel);  tv_date_sure = (TextView) findViewById(R.id.tv_date_sure);  tv_title = (TextView) findViewById(R.id.tv_title);  tv_title.setText(title);   mWheelYear = (WheelView) findViewById(R.id.wv_year);  mWheelMonth = (WheelView) findViewById(R.id.wv_month);  mWheelDay = (WheelView) findViewById(R.id.wv_day);   // 格式化当前时间,并转换为年月日整型数据  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());  String[] split = sdf.format(new Date()).split("-");  int currentYear = Integer.parseInt(split[0]);  int currentMonth = Integer.parseInt(split[1]);  int currentDay = Integer.parseInt(split[2]);   // 设置默认年月日为当前日期  mWheelYear.setData(getYearData(currentYear));  mWheelYear.setDefault(0);  mWheelMonth.setData(getMonthData());  mWheelMonth.setDefault(currentMonth - 1);  mWheelDay.setData(getDayData(getLastDay(currentYear, currentMonth)));  mWheelDay.setDefault(currentDay - 1);   mWheelYear.setOnSelectListener(this);  mWheelMonth.setOnSelectListener(this);  mWheelDay.setOnSelectListener(this);  } /**  * 年范围在:1900~今年  *  * @param currentYear  * @return  */  private ArrayList<String> getYearData(int currentYear) {
        ArrayList<String> list = new ArrayList<>();  for (int i = currentYear; i >= 1900; i--) {
            list.add(String.valueOf(i));  } return list;  } private ArrayList<String> getMonthData() {
        ArrayList<String> list = new ArrayList<>();  for (int i = 1; i <= 12; i  ) {
            list.add(String.valueOf(i));  } return list;  } /**  * 日范围在1~lastDay  *  * @param lastDay  * @return  */  private ArrayList<String> getDayData(int lastDay) { //ignore condition  ArrayList<String> list = new ArrayList<>();  for (int i = 1; i <= lastDay; i  ) {
            list.add(String.valueOf(i));  } return list;  } /**  * 判断是否闰年  *  * @param year  * @return  */  private boolean isLeapYear(int year) { return (year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);  } /**  * 获取特定年月对应的天数  *  * @param year  * @param month  * @return  */  private int getLastDay(int year, int month) { if (month == 2) { // 2月闰年的话返回29,防止28  return isLeapYear(year) ? 29 : 28;  } // 一三五七八十腊,三十一天永不差  return month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ? 31 : 30;  } @Override  public void endSelect(View view, int id, String text) { // 滚轮滑动停止后调用  switch (view.getId()) { case R.id.wv_year:{
                getYear();  } case R.id.wv_month: {
                getMonth();  // 记录当前选择的天数  int selectDay = Integer.parseInt(getDay());  // 根据当前选择的年月获取对应的天数  int lastDay = getLastDay(Integer.parseInt(getYear()), Integer.parseInt(getMonth()));  // 设置天数  mWheelDay.setData(getDayData(lastDay));  // 如果选中的天数大于实际天数,那么将默认天数设为实际天数;否则还是设置默认天数为选中的天数  if (selectDay > lastDay) { mWheelDay.setDefault(lastDay - 1);  } else { mWheelDay.setDefault(selectDay - 1);  }
            } case R.id.wv_day: {
                getDay();  }
            getDate();  }
    } @Override  public void selecting(View view, int id, String text) {
    } /**  * 设置确定按钮和取消被点击的接口  */  public interface onYesOnclickListener { public void onYesClick();  } public interface onNoOnclickListener { public void onNoClick();  } //接口回调,返回日期数据  public interface CallBackListener { public void getDate(String date);  } public void setCallBackListener(CallBackListener mCallBackListener){ this.mCallBackListener = mCallBackListener;  }

标签: 日期 Dialog 选择器

实例下载地址

自定义dialog实现日期选择器

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警