Easy to implement extensible custom Android wheel Time selection control _android

Source: Internet
Author: User
Tags getcolor int size time and date

There is a functional module in the project needs to use time to select the control, but the Android system itself is too ugly, can only optimize their own, combined with Wheelview to achieve the wheel selection date, as if the online also quite a lot of this article. But the scope of application is still different, I hope this will be able to demand the same friends have some help. Control title There are days and seconds of these can control whether they show, first look at the effect.

1. The start time of date

2. Only the end of the month and year

3. For sometimes the identity card expiration Time selection (divided into long and direct selection time of two, in addition to the long-term can also be extended later)

4. Project Structure

5. Direct code, code comments in detail

 <span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:14PX;"


>package Com.andrew.datechoosewheelviewdemo;
Import android.app.Activity;
Import Android.app.Dialog;
Import Android.content.Context;
Import Android.os.Looper;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.Button;
Import Android.widget.LinearLayout;

Import Android.widget.TextView;
Import Com.andrew.datechoosewheelviewdemo.widget.OnWheelChangedListener;
Import Com.andrew.datechoosewheelviewdemo.widget.OnWheelScrollListener;
Import Com.andrew.datechoosewheelviewdemo.widget.WheelView;

Import Com.andrew.datechoosewheelviewdemo.widget.adapters.AbstractWheelTextAdapter;
Import java.util.ArrayList;

Import Java.util.Calendar;  /** * Use instructions: 1.showLongTerm () whether to display long-term options * 2.setTimePickerGone hidden Time selection * 3. Interface Datechooseinterface * * For time and date selection * Created by
 Liuhongxia on 2016/4/16. * * public class Datechoosewheelviewdialog extends Dialog implements View.onclicKlistener {//control private Wheelview Myearwheelview;
 Private Wheelview Mdatewheelview;
 Private Wheelview Mhourwheelview;
 Private Wheelview Mminutewheelview;
 Private Calendartextadapter Mdateadapter;
 Private Calendartextadapter Mhouradapter;
 Private Calendartextadapter Mminuteadapter;
 Private Calendartextadapter Myearadapter;
 Private TextView Mtitletextview;
 Private Button Msurebutton;
 Private Dialog Mdialog;
 Private Button Mclosedialog;
 Private LinearLayout mlongtermlayout;

 Private TextView Mlongtermtextview;
 Variable private arraylist<string> arry_date = new arraylist<string> ();
 Private arraylist<string> Arry_hour = new arraylist<string> ();
 Private arraylist<string> Arry_minute = new arraylist<string> ();

 Private arraylist<string> arry_year = new arraylist<string> ();
 private int nowdateid = 0;
 private int nowhourid = 0;
 private int nowminuteid = 0;
 private int nowyearid = 0;
 Private String myearstr; Private String MDATESTR;
 Private String mhourstr;
 Private String mminutestr; Private Boolean mblnbelongterm = false;//Whether a long term private Boolean mblntimepickergone = false;//Time Select whether to display//constant private fi
 nal int max_text_size = 18;

 Private final int min_text_size = 16;
 Private context Mcontext;

 Private Datechooseinterface Datechooseinterface;
 Public Datechoosewheelviewdialog (context, Datechooseinterface Datechooseinterface) {super (context);
 This.mcontext = context;
 This.datechooseinterface = Datechooseinterface;
 Mdialog = new Dialog (context,r.style.dialog);
 Initview ();
 InitData ();
 private void InitData () {inityear ();
 Initdate ();
 Inithour ();
 Initminute ();
 Initlistener (); /** * Initialization rolling listener event/private void Initlistener () {//year ***************************** Myearwheelview.addchanginglist
  Ener (New Onwheelchangedlistener () {@Override public void onchanged (Wheelview wheel, int oldValue, int newvalue) { String currenttext = (string) myearadapter.getitemtext (WHEEL.GEtcurrentitem ());
  Settextviewstyle (Currenttext, Myearadapter);
  Myearstr = Arry_year.get (Wheel.getcurrentitem ()) + "";

 }
 });  Myearwheelview.addscrollinglistener (New Onwheelscrolllistener () {@Override public void onscrollingstarted (Wheelview Wheel) {} @Override public void onscrollingfinished (Wheelview wheel) {String currenttext = (string) myearadap
  Ter.getitemtext (Wheel.getcurrentitem ());
  Settextviewstyle (Currenttext, Myearadapter);

 }
 }); Date ******************** Mdatewheelview.addchanginglistener (new Onwheelchangedlistener () {@Override public void OnC Hanged (Wheelview wheel, int oldValue, int newvalue) {String Currenttext = (String) mdateadapter.getitemtext (WHEEL.GETCU
  Rrentitem ());
Settextviewstyle (Currenttext, Mdateadapter);
  Mdatecalendartextview.settext ("" + Arry_date.get (Wheel.getcurrentitem ()));
  Mdatestr = Arry_date.get (Wheel.getcurrentitem ());

 }
 }); Mdatewheelview.addscrollinglistener (New Onwheelscrolllistener () {@Override public void onscrollingstarted (Wheelview wheel) {} @Override public void onscrollingfinished (Wheelview wheel)
  {String currenttext = (string) mdateadapter.getitemtext (Wheel.getcurrentitem ());
  Settextviewstyle (Currenttext, Mdateadapter);

 }
 }); Hours *********************************** Mhourwheelview.addchanginglistener (new Onwheelchangedlistener () {@ Override public void onchanged (Wheelview wheel, int oldValue, int newvalue) {String Currenttext = (String) mhouradapt
  Er.getitemtext (Wheel.getcurrentitem ());
  Settextviewstyle (Currenttext, Mhouradapter);
  Mhourstr = Arry_hour.get (Wheel.getcurrentitem ()) + "";

 }
 });  Mhourwheelview.addscrollinglistener (New Onwheelscrolllistener () {@Override public void onscrollingstarted (Wheelview Wheel) {} @Override public void onscrollingfinished (Wheelview wheel) {String currenttext = (string) mhouradap
  Ter.getitemtext (Wheel.getcurrentitem ());
  Settextviewstyle (Currenttext, Mhouradapter);

 }
 }); MinutesMminutewheelview.addchanginglistener (New Onwheelchangedlistener () {@ Override public void onchanged (Wheelview wheel, int oldValue, int newvalue) {String Currenttext = (String) Mminuteada
  Pter.getitemtext (Wheel.getcurrentitem ());
  Settextviewstyle (Currenttext, Mminuteadapter);
  Mminutestr = Arry_minute.get (Wheel.getcurrentitem ()) + "";

 }
 }); Mminutewheelview.addscrollinglistener (New Onwheelscrolllistener () {@Override public void onscrollingstarted (WheelVi EW wheel) {} @Override public void onscrollingfinished (Wheelview wheel) {String currenttext = (string) mminute
  Adapter.getitemtext (Wheel.getcurrentitem ());
  Settextviewstyle (Currenttext, Mminuteadapter);
 }
 });
 /** * Initialization minutes/private void Initminute () {Calendar Nowcalendar = calendar.getinstance ();
 int nowminite = Nowcalendar.get (Calendar.minute);
 Arry_minute.clear ();
  for (int i = 0; I <= i++) {arry_minute.add (i + ""); if (Nowminite == i) {Nowminuteid = Arry_minute.size ()-1;
 } mminuteadapter = new Calendartextadapter (Mcontext, Arry_minute, Nowminuteid, Max_text_size, MIN_TEXT_SIZE);
 Mminutewheelview.setvisibleitems (5);
 Mminutewheelview.setviewadapter (Mminuteadapter);
 Mminutewheelview.setcurrentitem (Nowminuteid);
 Mminutestr = Arry_minute.get (Nowminuteid) + "";

 Settextviewstyle (Mminutestr, Mminuteadapter);
 /** * Initialization time */private void Inithour () {Calendar Nowcalendar = calendar.getinstance ();
 int nowhour = Nowcalendar.get (Calendar.hour_of_day);
 Arry_hour.clear ();
  for (int i = 0; I <= i++) {arry_hour.add (i + "");
  if (Nowhour = i) {Nowhourid = Arry_hour.size ()-1;
 } mhouradapter = new Calendartextadapter (Mcontext, Arry_hour, Nowhourid, Max_text_size, min_text_size);
 Mhourwheelview.setvisibleitems (5);
 Mhourwheelview.setviewadapter (Mhouradapter);
 Mhourwheelview.setcurrentitem (Nowhourid);
 Mhourstr = Arry_hour.get (Nowhourid) + ""; Settextviewstyle (Mhourstr, Mhouradapter);
 /** * Initialization year/private void Inityear () {Calendar Nowcalendar = calendar.getinstance ();
 int nowyear = Nowcalendar.get (calendar.year);
 Arry_year.clear ();
  for (int i = 0; I <=. i++) {int year = nowYear-30 + i;
  Arry_year.add (Year + "Years");
  if (Nowyear = = year) {Nowyearid = Arry_year.size ()-1;
 } myearadapter = new Calendartextadapter (Mcontext, Arry_year, Nowyearid, Max_text_size, min_text_size);
 Myearwheelview.setvisibleitems (5);
 Myearwheelview.setviewadapter (Myearadapter);
 Myearwheelview.setcurrentitem (Nowyearid);
 Myearstr = Arry_year.get (Nowyearid);
 private void Initview () {View view = Layoutinflater.from (Mcontext). Inflate (r.layout.dialog_date_choose, NULL);
 Mdialog.setcontentview (view);
 Myearwheelview = (Wheelview) View.findviewbyid (R.ID.YEAR_WV);
 Mdatewheelview = (Wheelview) View.findviewbyid (R.ID.DATE_WV);
 Mhourwheelview = (Wheelview) View.findviewbyid (R.ID.HOUR_WV); Mminutewheelview = (Wheelview) View.findviewbyid (R.ID.MINUTE_WV);
 Mtitletextview = (TextView) View.findviewbyid (R.ID.TITLE_TV);
 Msurebutton = (Button) View.findviewbyid (R.ID.SURE_BTN);
 Mclosedialog = (Button) View.findviewbyid (R.ID.DATE_CHOOSE_CLOSE_BTN);
 Mlongtermlayout = (linearlayout) View.findviewbyid (r.id.long_term_layout);

 Mlongtermtextview = (TextView) View.findviewbyid (R.ID.LONG_TERM_TV);
 Msurebutton.setonclicklistener (this);
 Mclosedialog.setonclicklistener (this);
 Mlongtermtextview.setonclicklistener (this);
 }/** * Initialization date/private void Initdate () {Calendar Nowcalendar = calendar.getinstance ();
 int nowyear = Nowcalendar.get (calendar.year);
 Arry_date.clear ();
 Setdate (nowyear);
 Mdateadapter = new Calendartextadapter (Mcontext, Arry_date, Nowdateid, Max_text_size, min_text_size);
 Mdatewheelview.setvisibleitems (5);
 Mdatewheelview.setviewadapter (Mdateadapter);

 Mdatewheelview.setcurrentitem (Nowdateid);
 Mdatestr = Arry_date.get (Nowdateid);
 Settextviewstyle (Mdatestr, Mdateadapter); } public void Setdatedialogtitle (string title) {Mtitletextview.settext (title);
 } public void Settimepickergone (Boolean isgone) {mblntimepickergone = Isgone; if (isgone) {linearlayout.layoutparams yearparams = new Linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_
  CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

  Yearparams.rightmargin = 22;
   Linearlayout.layoutparams dateparams = new Linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT,
  LinearLayout.LayoutParams.WRAP_CONTENT);
  Myearwheelview.setlayoutparams (Yearparams);

  Mdatewheelview.setlayoutparams (Dateparams);
  Mhourwheelview.setvisibility (View.gone);
 Mminutewheelview.setvisibility (View.gone);
  else {mhourwheelview.setvisibility (view.visible);
 Mminutewheelview.setvisibility (view.visible);
 } public void Showlongterm (a Boolean show) {if (show) {mlongtermlayout.setvisibility (view.visible);
 else {mlongtermlayout.setvisibility (view.gone); }/** * Writes all dates of the change year to the array * @param years/private void SETdate (int year) {Boolean isrun = Isrunnian (year);
 Calendar Nowcalendar = Calendar.getinstance ();
 int nowmonth = Nowcalendar.get (calendar.month) + 1;
 int nowday = Nowcalendar.get (calendar.day_of_month); for (int month = 1; month <= month++) {switch (month) {case 1:case 3:case 5:case 7:case 8:case 1

   0:case 12:for (int day = 1; days <= to day++) {arry_date.add (month + "month" + Day + "th");
   if (month = = Nowmonth && Day = = nowday) {Nowdateid = Arry_date.size ()-1;
  }} break;
    Case 2:if (isrun) {for int days = 1; day <=; day++) {Arry_date.add (month + "month" + Days + "th");
    if (month = = Nowmonth && Day = = nowday) {Nowdateid = Arry_date.size ()-1;
    }}else {for (int day = 1; days <= day++) {arry_date.add (month + "month" + Day + "th");
    if (month = = Nowmonth && Day = = nowday) {Nowdateid = Arry_date.size ()-1;
  }} break;
 Case 4:case 6:case 9: Case 11:for (int day = 1; days <= day++) {arry_date.add (month + "month" + Day + "th");
   if (month = = Nowmonth && Day = = nowday) {Nowdateid = Arry_date.size ()-1;
  }} break;
  Default:break; }}/** * Determine if it is a leap year * @param year * @return/private Boolean Isrunnian (int year) {if (year% 4 = 0 && Year% 100!=0 | |
 Year% = = 0) {return true;
 }else {return false; /** * Sets the size of the text * @param curriteitemtext * @param adapter * * public void Settextviewstyle (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 (max_text_size);
  Textvew.settextcolor (Mcontext.getresources (). GetColor (r.color.text_10)); else {TexTvew.settextsize (min_text_size);
  Textvew.settextcolor (Mcontext.getresources (). GetColor (R.color.text_11)); @Override public void OnClick (View v) {switch (V.getid ()) {case r.id.sure_btn://determines the Select button to listen for if (mblntimepic
  Kergone) {datechooseinterface.getdatetime (Strtimetodateformat (Myearstr, Mdatestr), mblnbelongterm); else {datechooseinterface.getdatetime (Strtimetodateformat (Myearstr, Mdatestr, Mhourstr, mminutestr), MBlnBeLongTer
  m);
  } dismissdialog ();
  Break
  Case r.id.date_choose_close_btn://Close Date selection dialog box DismissDialog ();
  Break Case r.id.long_term_tv://Select long time to monitor if (!mblnbelongterm) {Mlongtermtextview.setbackgroundresource (
   R.drawable.gouxuanok);
  Mblnbelongterm = true;
   else {mlongtermtextview.setbackgroundresource (R.drawable.gouxuanno);
  Mblnbelongterm = false;
 } Default:break;
 }/** * Dialog box disappears * * private void DismissDialog () {if (Looper.mylooper ()!= looper.getmainlooper ()) {return; } if (null = = Mdialog | |!mdialog.isshowing () | | Null = = Mcontext | |
 (activity) mcontext). Isfinishing ()) {return;
 } Mdialog.dismiss ();
 This.dismiss ();

  /** * Display Date Selection Dialog */public void Showdatechoosedialog () {if (Looper.mylooper ()!= looper.getmainlooper ()) {
 Return } if (null = = Mcontext | |
 (activity) mcontext). Isfinishing ()) {//interface has been destroyed return;
  } if (null!= mdialog) {mdialog.show ();
 Return
 } if (null = = Mdialog) {return;
 } mdialog.setcanceledontouchoutside (True);
 Mdialog.show (); /** * xx years xx xx days xx yyyy-mm-dd hh:mm * @param yearstr * @param datestr * @param hourstr * @param minutestr * @return */private string Strtimetodateformat (String yearstr, String datestr, String hourstr, String minutestr) {Retu
 RN Yearstr.replace ("Year", "-") + Datestr.replace ("month", "-"). Replace ("Day", "") + Hourstr + ":" + minutestr; private string Strtimetodateformat (String yearstr, String datestr) {return Yearstr.replace ("year", "-") + DATESTR.REPL Ace ("Month", "-"). Replace ("Day", ""); }/** * Adapter/Private class Calendartextadapter extends Abstractwheeltextadapter {arraylist<string> l

 Ist 
  Protected Calendartextadapter (context, arraylist<string> list, int currentitem, int maxsize, int minsize) {
  Super (context, R.layout.item_birth_year, R.id.tempvalue, CurrentItem, MaxSize, minsize);
 This.list = list; @Override public View getitem (int index, View cachedview, ViewGroup parent) {View view = Super.getitem (Index, Cach
  Edview, parent);
 return view;
 @Override public int Getitemscount () {return list.size ();
  @Override protected charsequence getitemtext (int index) {String str = list.get (index) + "";
 return str; }/** * Callback selected time (Default time format "YYYY-MM-DD HH:mm:ss") */public interface datechooseinterface{void GetDateTime (String Tim
 E, Boolean longtimechecked);

 }} </span></span>

6.MainActivity inside use

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:14PX;"

>package Com.andrew.datechoosewheelviewdemo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;

Import Android.widget.TextView;
 public class Mainactivity extends activity implements View.onclicklistener {private Button Mstartdatebutton;
 Private Button Menddatebutton;
 Private Button Mdatevalidbutton;

 Private TextView Mshowcontenttextview;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

 Setcontentview (R.layout.activity_main);
 Mstartdatebutton = (Button) This.findviewbyid (R.ID.START_DATE_BTN);
 Menddatebutton = (Button) This.findviewbyid (R.ID.END_DATE_BTN);
 Mdatevalidbutton = (Button) This.findviewbyid (R.ID.DATE_VALID_BTN);

 Mshowcontenttextview = (TextView) This.findviewbyid (R.ID.SHOW_CONTENT_TV);
 Mstartdatebutton.setonclicklistener (this);
 Menddatebutton.setonclicklistener (this); MdatevaliDbutton.setonclicklistener (this); @Override public void OnClick (View v) {switch (V.getid ()) {case r.id.start_date_btn://start time Datechoosewheelview Dialog Startdatechoosedialog = new Datechoosewheelviewdialog (Mainactivity.this, new Datechoosewheelviewdialog.datechooseinterface () {@Override public void GetDateTime (String time, Boolean Longtimeche
   cked) {Mshowcontenttextview.settext (time);
  }
  });
  Startdatechoosedialog.setdatedialogtitle ("Start Time");
  Startdatechoosedialog.showdatechoosedialog ();
  Break Case r.id.end_date_btn://End time Datechoosewheelviewdialog enddatechoosedialog = new Datechoosewheelviewdialog ( Mainactivity.this, New Datechoosewheelviewdialog.datechooseinterface () {@Override public void GetDateTime (stri
    Ng time, Boolean longtimechecked} {Mshowcontenttextview.settext (time);
  }
   });
  Enddatechoosedialog.settimepickergone (TRUE);
  Enddatechoosedialog.setdatedialogtitle ("End Time");
  Enddatechoosedialog.showdatechoosedialog (); BrEak Case r.id.date_valid_btn://ID card validity datechoosewheelviewdialog datevalidchoosedialog = new Datechoosewheelviewdialog ( Mainactivity.this, New Datechoosewheelviewdialog.datechooseinterface () {@Override public void GetDateTime (stri
    Ng time, Boolean longtimechecked) {if (longtimechecked) {mshowcontenttextview.settext ("long term");
    else {mshowcontenttextview.settext (time);
  }
    }
   });
  Datevalidchoosedialog.settimepickergone (TRUE);
  Datevalidchoosedialog.showlongterm (TRUE);
  Datevalidchoosedialog.setdatedialogtitle ("Identity card Expiration Time");
  Datevalidchoosedialog.showdatechoosedialog ();
  Break
 Default:break;

 }} </span></span>

Code Download Address:
Github:https://github.com/hongxialiu/datechoosewheelviewdemo
SOURCE Download: Wheelview Wheel Time selection control

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.