Android日期选择器实现年月日三级联动

Android日期选择器实现年月日三级联动,第1张

概述最近项目里面用到了一个日期选择器,实现现在主流的WheelView滑动选择,整理了下,做了个Demo.废话不多说,直接上代码.

最近项目里面用到了一个日期选择器,实现现在主流的WheelVIEw滑动选择,整理了下,做了个Demo.废话不多说,直接上代码.

主布局:activity_main.xml

<?xml version="1.0" enCoding="utf-8"?> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   xmlns:tools="http://schemas.androID.com/tools"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:paddingBottom="@dimen/activity_vertical_margin"   androID:paddingleft="@dimen/activity_horizontal_margin"   androID:paddingRight="@dimen/activity_horizontal_margin"   androID:paddingtop="@dimen/activity_vertical_margin"   tools:context="com.example.user.mydate.MainActivity">    <TextVIEw     androID:ID="@+ID/selectDate"     androID:layout_wIDth="match_parent"     androID:layout_height="30dp"     androID:gravity="center"     androID:onClick="selectDate"     androID:clickable="true"     androID:text="日期选择" /> </relativeLayout> 

 MainActivity:

package com.example.user.mydate;  import androID.app.Activity; import androID.os.Bundle; import androID.vIEw.Gravity; import androID.vIEw.VIEw; import androID.vIEw.Window; import androID.Widget.TextVIEw; import androID.Widget.Toast;  import com.example.user.mydate.vIEw.ChangeDatePopwindow;  public class MainActivity extends Activity {    private TextVIEw selectDate;   @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     requestwindowFeature(Window.FEATURE_NO_Title);     setContentVIEw(R.layout.activity_main);     selectDate= (TextVIEw) findVIEwByID(R.ID.selectDate);   }    public voID selectDate(VIEw v){     if (v.getID()==R.ID.selectDate){       selectDate();     }    }     private String[] selectDate() {     final String[] str = new String[10];     ChangeDatePopwindow mChangeBirthDialog = new ChangeDatePopwindow(         this);     mChangeBirthDialog.setDate("2016","1","1");     mChangeBirthDialog.showAtLocation(selectDate,Gravity.BottOM,0);     mChangeBirthDialog.setBirthdayListener(new ChangeDatePopwindow.OnBirthListener() {        @OverrIDe       public voID onClick(String year,String month,String day) {         // Todo auto-generated method stub         Toast.makeText(MainActivity.this,year + "-" + month + "-" + day,Toast.LENGTH_LONG).show();         StringBuilder sb = new StringBuilder();         sb.append(year.substring(0,year.length() - 1)).append("-").append(month.substring(0,day.length() - 1)).append("-").append(day);         str[0] = year + "-" + month + "-" + day;         str[1] = sb.toString();         selectDate.setText(year + "-" + month + "-" + day);        }     });     return str;   }  } 

主要的类:

package com.example.user.mydate.vIEw;  import androID.content.Context; import androID.graphics.drawable.colorDrawable; import androID.util.Log; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.Widget.PopupWindow; import androID.Widget.TextVIEw;  import com.example.user.mydate.R; import com.example.user.mydate.vIEw.wheelvIEw.OnWheelChangedListener; import com.example.user.mydate.vIEw.wheelvIEw.OnWheelScrollListener; import com.example.user.mydate.vIEw.wheelvIEw.WheelVIEw; import com.example.user.mydate.vIEw.wheelvIEw.adapter.AbstractWheelTextAdapter1;  import java.util.ArrayList; import java.util.Calendar;  /**  * Author: Chen.yuan  * Email:  hubeiqiyuan2010@163.com  * Date:  2016/7/28 17:37  * Description:日期选择window  */ public class ChangeDatePopwindow extends PopupWindow implements VIEw.OnClickListener {    private Context context;   private WheelVIEw wvYear;   private WheelVIEw wvMonth;   private WheelVIEw wvDay;    private TextVIEw btnSure;   private TextVIEw btnCancel;    private ArrayList<String> arry_years = new ArrayList<String>();   private ArrayList<String> arry_months = new ArrayList<String>();   private ArrayList<String> arry_days = new ArrayList<String>();   private CalendarTextAdapter mYearadapter;   private CalendarTextAdapter mMonthAdapter;   private CalendarTextAdapter mDaydapter;    private String month;   private String day;    private String currentYear = getYear();   private String currentMonth = getMonth();   private String currentDay = getDay();    private int maxTextSize = 24;   private int minTextSize = 14;    private boolean issetdata = false;    private String selectYear;   private String selectMonth;   private String selectDay;    private OnBirthListener onBirthListener;    public ChangeDatePopwindow(final Context context) {     super(context);     this.context = context;     VIEw vIEw=VIEw.inflate(context,R.layout.dialog_myinfo_changebirth,null);     wvYear = (WheelVIEw) vIEw.findVIEwByID(R.ID.wv_birth_year);     wvMonth = (WheelVIEw) vIEw.findVIEwByID(R.ID.wv_birth_month);     wvDay = (WheelVIEw) vIEw.findVIEwByID(R.ID.wv_birth_day);     btnSure = (TextVIEw) vIEw.findVIEwByID(R.ID.btn_myinfo_sure);     btnCancel = (TextVIEw) vIEw.findVIEwByID(R.ID.btn_myinfo_cancel);      //设置SelectPicPopupWindow的VIEw     this.setContentVIEw(vIEw);     //设置SelectPicPopupWindowd出窗体的宽     this.setWIDth(VIEwGroup.LayoutParams.MATCH_PARENT);     //设置SelectPicPopupWindowd出窗体的高     this.setHeight(VIEwGroup.LayoutParams.MATCH_PARENT);     //设置SelectPicPopupWindowd出窗体可点击     this.setFocusable(true);     //设置SelectPicPopupWindowd出窗体动画效果 //   this.setAnimationStyle(R.style.AnimBottom);     //实例化一个colorDrawable颜色为半透明     colorDrawable DW = new colorDrawable(0xb0000000);     //设置SelectPicPopupWindowd出窗体的背景     this.setBackgroundDrawable(DW);      btnSure.setonClickListener(this);     btnCancel.setonClickListener(this);      if (!issetdata) {       initData();     }     initYears();     mYearadapter = new CalendarTextAdapter(context,arry_years,setYear(currentYear),maxTextSize,minTextSize);     wvYear.setVisibleItems(5);     wvYear.setVIEwAdapter(mYearadapter);     wvYear.setCurrentItem(setYear(currentYear));      initMonths(Integer.parseInt(month));     mMonthAdapter = new CalendarTextAdapter(context,arry_months,setMonth(currentMonth),minTextSize);     wvMonth.setVisibleItems(5);     wvMonth.setVIEwAdapter(mMonthAdapter);     wvMonth.setCurrentItem(setMonth(currentMonth));      initDays(Integer.parseInt(day));     mDaydapter = new CalendarTextAdapter(context,arry_days,Integer.parseInt(currentDay) - 1,minTextSize);     wvDay.setVisibleItems(5);     wvDay.setVIEwAdapter(mDaydapter);     wvDay.setCurrentItem(Integer.parseInt(currentDay) - 1);      wvYear.addChangingListener(new OnWheelChangedListener() {        @OverrIDe       public voID onChanged(WheelVIEw wheel,int oldValue,int newValue) {         // Todo auto-generated method stub         String currentText = (String) mYearadapter.getItemText(wheel.getCurrentItem());         selectYear = currentText;         setTextvIEwSize(currentText,mYearadapter);         currentYear = currentText.substring(0,currentText.length()-1).toString();         Log.d("currentYear==",currentYear);         setYear(currentYear);         initMonths(Integer.parseInt(month));         mMonthAdapter = new CalendarTextAdapter(context,minTextSize);         wvMonth.setVisibleItems(5);         wvMonth.setVIEwAdapter(mMonthAdapter);         wvMonth.setCurrentItem(0);          calDays(currentYear,month);       }     });      wvYear.addScrollingListener(new OnWheelScrollListener() {        @OverrIDe       public voID onScrollingStarted(WheelVIEw wheel) {         // Todo auto-generated method stub        }        @OverrIDe       public voID onScrollingFinished(WheelVIEw wheel) {         // Todo auto-generated method stub         String currentText = (String) mYearadapter.getItemText(wheel.getCurrentItem());         setTextvIEwSize(currentText,mYearadapter);       }     });      wvMonth.addChangingListener(new OnWheelChangedListener() {        @OverrIDe       public voID onChanged(WheelVIEw wheel,int newValue) {         // Todo auto-generated method stub         String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem());         selectMonth = currentText;         setTextvIEwSize(currentText,mMonthAdapter);         setMonth(currentText.substring(0,1));         initDays(Integer.parseInt(day));         mDaydapter = new CalendarTextAdapter(context,minTextSize);         wvDay.setVisibleItems(5);         wvDay.setVIEwAdapter(mDaydapter);         wvDay.setCurrentItem(0);          calDays(currentYear,month);       }     });      wvMonth.addScrollingListener(new OnWheelScrollListener() {        @OverrIDe       public voID onScrollingStarted(WheelVIEw wheel) {         // Todo auto-generated method stub        }        @OverrIDe       public voID onScrollingFinished(WheelVIEw wheel) {         // Todo auto-generated method stub         String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem());         setTextvIEwSize(currentText,mMonthAdapter);       }     });      wvDay.addChangingListener(new OnWheelChangedListener() {        @OverrIDe       public voID onChanged(WheelVIEw wheel,int newValue) {         // Todo auto-generated method stub         String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem());         setTextvIEwSize(currentText,mDaydapter);         selectDay = currentText;       }     });      wvDay.addScrollingListener(new OnWheelScrollListener() {        @OverrIDe       public voID onScrollingStarted(WheelVIEw wheel) {         // Todo auto-generated method stub        }        @OverrIDe       public voID onScrollingFinished(WheelVIEw wheel) {         // Todo auto-generated method stub         String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem());         setTextvIEwSize(currentText,mDaydapter);       }     });   }     public voID initYears() {     for (int i = Integer.parseInt(getYear()); i > 1950; i--) {       arry_years.add(i + "年");     }   }    public voID initMonths(int months) {     arry_months.clear();     for (int i = 1; i <= months; i++) {       arry_months.add(i + "月");     }   }    public voID initDays(int days) {     arry_days.clear();     for (int i = 1; i <= days; i++) {       arry_days.add(i + "日");     }   }    private class CalendarTextAdapter extends AbstractWheelTextAdapter1 {     ArrayList<String> List;      protected CalendarTextAdapter(Context context,ArrayList<String> List,int currentItem,int maxsize,int minsize) {       super(context,R.layout.item_birth_year,NO_RESOURCE,currentItem,maxsize,minsize);       this.List = List;       setItemTextResource(R.ID.tempValue);     }      @OverrIDe     public VIEw getItem(int index,VIEw cachedVIEw,VIEwGroup parent) {       VIEw vIEw = super.getItem(index,cachedVIEw,parent);       return vIEw;     }      @OverrIDe     public int getItemsCount() {       return List.size();     }      @OverrIDe     protected CharSequence getItemText(int index) {       return List.get(index) + "";     }   }    public voID setBirthdayListener(OnBirthListener onBirthListener) {     this.onBirthListener = onBirthListener;   }    @OverrIDe   public voID onClick(VIEw v) {      if (v == btnSure) {       if (onBirthListener != null) {         onBirthListener.onClick(selectYear,selectMonth,selectDay);         Log.d("cy",""+selectYear+""+selectMonth+""+selectDay);       }     } else if (v == btnSure) {      } else {       dismiss();     }     dismiss();    }    public interface OnBirthListener {     public voID onClick(String year,String day);   }    /**    * 设置字体大小    *    * @param curriteItemText    * @param adapter    */   public voID setTextvIEwSize(String curriteItemText,CalendarTextAdapter adapter) {     ArrayList<VIEw> arrayList = adapter.getTestVIEws();     int size = arrayList.size();     String currentText;     for (int i = 0; i < size; i++) {       TextVIEw textvew = (TextVIEw) arrayList.get(i);       currentText = textvew.getText().toString();       if (curriteItemText.equals(currentText)) {         textvew.setTextSize(maxTextSize);       } else {         textvew.setTextSize(minTextSize);       }     }   }    public String getYear() {     Calendar c = Calendar.getInstance();     return c.get(Calendar.YEAR)+"";   }    public String getMonth() {     Calendar c = Calendar.getInstance();     return c.get(Calendar.MONTH) + 1+"";   }    public String getDay() {     Calendar c = Calendar.getInstance();     return c.get(Calendar.DATE)+"";   }    public voID initData() {     setDate(getYear(),getMonth(),getDay());     this.currentDay = 1+"";     this.currentMonth = 1+"";   }    /**    * 设置年月日    *    * @param year    * @param month    * @param day    */   public voID setDate(String year,String day) {     selectYear = year + "年";     selectMonth = month + "月";     selectDay = day + "日";     issetdata = true;     this.currentYear = year;     this.currentMonth = month;     this.currentDay = day;     if (year == getYear()) {       this.month = getMonth();     } else {       this.month = 12+"";     }     calDays(year,month);   }    /**    * 设置年份    *    * @param year    */   public int setYear(String year) {     int yearIndex = 0;     if (!year.equals(getYear())) {       this.month = 12+"";     } else {       this.month = getMonth();     }     for (int i = Integer.parseInt(getYear()); i > 1950; i--) {       if (i == Integer.parseInt(year)) {         return yearIndex;       }       yearIndex++;     }     return yearIndex;   }    /**    * 设置月份    *    * @param month    * @param month    * @return    */   public int setMonth(String month) {     int monthIndex = 0;     calDays(currentYear,month);     for (int i = 1; i < Integer.parseInt(this.month); i++) {       if (Integer.parseInt(month) == i) {         return monthIndex;       } else {         monthIndex++;       }     }     return monthIndex;   }    /**    * 计算每月多少天    *    * @param month    * @param year    */   public voID calDays(String year,String month) {     boolean leayyear = false;     if (Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0) {       leayyear = true;     } else {       leayyear = false;     }     for (int i = 1; i <= 12; i++) {       switch (Integer.parseInt(month)) {       case 1:       case 3:       case 5:       case 7:       case 8:       case 10:       case 12:         this.day = 31+"";         break;       case 2:         if (leayyear) {           this.day = 29+"";         } else {           this.day = 28+"";         }         break;       case 4:       case 6:       case 9:       case 11:         this.day = 30+"";         break;       }     }     if (year.equals( getYear()) && month .equals( getMonth())) {       this.day = getDay();     }   } } 

底部d出框布局:

<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/ly_myinfo_changeaddress" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@color/color_transparent" androID:gravity="bottom" androID:orIEntation="vertical" >  <linearLayout   androID:ID="@+ID/ly_myinfo_changeaddress_child"   androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   androID:layout_alignParentBottom="true"   androID:background="@color/white"   androID:orIEntation="vertical" >     <relativeLayout     androID:layout_wIDth="match_parent"     androID:layout_height="44dp"     >      <TextVIEw       androID:ID="@+ID/btn_myinfo_cancel"       androID:layout_wIDth="wrap_content"       androID:layout_height="match_parent"       androID:paddingleft="12dp"       androID:text="取消"       androID:gravity="center"       androID:layout_alignParentleft="true"       androID:layout_marginRight="15dip"       androID:textcolor="@color/color_e84515"       androID:textSize="13sp" />       <TextVIEw       androID:ID="@+ID/btn_myinfo_sure"       androID:layout_wIDth="wrap_content"       androID:layout_height="match_parent"       androID:layout_alignParentRight="true"       androID:gravity="center"       androID:text="完成"       androID:textcolor="@color/color_e84515"       androID:paddingRight="12dp"       androID:textSize="12sp" />    </relativeLayout>   <TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"     androID:layout_wIDth="match_parent"     androID:layout_height="0.5dp"     androID:background="@color/color_d8d8d8"     />   <linearLayout     androID:layout_wIDth="match_parent"     androID:layout_height="160dip"     androID:orIEntation="horizontal"     androID:gravity="center_vertical">     <com.example.user.mydate.vIEw.wheelvIEw.WheelVIEw       androID:ID="@+ID/wv_birth_year"       androID:layout_wIDth="0dip"       androID:layout_weight="1"       androID:layout_height="match_parent"       androID:layout_gravity="center_vertical"       />     <com.example.user.mydate.vIEw.wheelvIEw.WheelVIEw       androID:ID="@+ID/wv_birth_month"       androID:layout_wIDth="0dip"       androID:layout_weight="1"       androID:layout_height="match_parent"       androID:layout_gravity="center_vertical"       />      <com.example.user.mydate.vIEw.wheelvIEw.WheelVIEw       androID:ID="@+ID/wv_birth_day"       androID:layout_wIDth="0dip"       androID:layout_weight="1"       androID:layout_height="match_parent"       androID:layout_gravity="center_vertical"       />   </linearLayout>  </linearLayout>  </linearLayout> 

运行效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:Android 日期选择器实例代码Android自定义DataTimePicker实例代码(日期选择器)Android时间选择器、日期选择器实现代码Android 中NumberPicker,DatePicker与DatePickerDialog中分割颜色的修改实例代码android指定DatePickerDialog样式并不显示年的实现代码Android DatePicker和DatePickerDialog基本用法示例详解Android中提示对话框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)Android日期选择器对话框DatePickerDialog使用详解 总结

以上是内存溢出为你收集整理的Android日期选择器实现年月日三级联动全部内容,希望文章能够帮你解决Android日期选择器实现年月日三级联动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143044.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存