Android Custom Datatimepicker (date picker)

Source: Internet
Author: User

Android Custom Datatimepicker (date picker)

I have not published an article on Android for some time, about Android custom components I have a few of the author want to share with you, later will be recorded in the blog. This blog for everyone to share is to customize a date selector, you can allow users to select both the date and the current time.
first Look at the effect:

the effect is to display date selection and time selection on the same layout.
To customize a class:/datetimepicker/src/com/wwj/datetimepicker/datetimepickdialogutil.java
Package Com.wwj.datetimepicker;import Java.text.simpledateformat;import Java.util.calendar;import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.widget.datepicker;import Android.widget.datepicker.ondatechangedlistener;import Android.widget.EditText; Import Android.widget.linearlayout;import Android.widget.timepicker;import android.widget.timepicker.ontimechangedlistener;/** * Date Time selection control usage: Private EditText inputdate;//need to set the datetime text edit box Private String * initdatetime= "September 3, 2012 14:44",//The initial datetime value is used in the Click event: * Inputdate.setonclicklistener (New Onclicklistener {* * * @Override public void OnClick (View v) {datetimepickdialogutil * datetimepickdialog=new * Da Tetimepickdialogutil (Sinvestigateactivity.this,initdatetime); * Datetimepickdialog.datetimepickdialog (Inputdate); *  *           } }); * * @author */public class Datetimepickdialogutil implements Ondatechangedlistener,ontimechangedlistener {private DatePicker datepicker;private timepicker timepicker;private alertdialog ad;private string Datetime;private string Initdatetime;private Activity activity;/** * DateTime pop-up selection box constructor * * @param activity *: Called Parent activity * @param INITDA Tetime * Initial datetime value, as the title and datetime initial value of the popup window */public datetimepickdialogutil (activity activity, String initdatetime) {thi s.activity = Activity;this.initdatetime = Initdatetime;} public void Init (DatePicker DatePicker, Timepicker timepicker) {Calendar calendar = calendar.getinstance (); Null = = Initdatetime | | "". Equals (Initdatetime)) {calendar = This.getcalendarbyinintdata (initdatetime);} else {initdatetime = Calendar.get (calendar.year) + "year" + Calendar.get (calendar.month) + "month" + calendar.get (Calendar.DAY_ Of_month) + "Day" + calendar.get (calendar.hour_of_day) + ":" + calendar.get (Calendar.minute);} Datepicker.init (Calendar.get (calendar.year), Calendar.get (Calendar.month), Calendar.get (Calendar.DAY_OF_MONTH), this); Timepicker.setcurrenthour (Calendar.get (CalendaR.hour_of_day)); Timepicker.setcurrentminute (Calendar.get (Calendar.minute));} /** * Pop-up datetime selection Box method * * @param inputdate *: To set the date time text edit box * @return */public alertdialog Datetimepickdialog (fi NAL EditText inputdate) {linearlayout datetimelayout = (linearlayout) activity.getlayoutinflater (). Inflate ( R.layout.common_datetime, null);d Atepicker = (datePicker) Datetimelayout.findviewbyid (r.id.datepicker); timePicker = (Timepicker) Datetimelayout.findviewbyid (R.id.timepicker); Init (DatePicker, timepicker); Timepicker.setis24hourview (True); Timepicker.setontimechangedlistener (this); ad = new Alertdialog.builder (activity) . Settitle (Initdatetime). Setview (Datetimelayout). Setpositivebutton ("Settings", new Dialoginterface.onclicklistener () { public void OnClick (dialoginterface dialog, int whichbutton) {inputdate.settext (dateTime);}}). Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int Whichbutton) {Inputdate.settext ("");}}). Show (); Ondatechanged (null, 0, 0, 0); return AD;} public void ontimechanged (timepicker view, int hourofday, int minute) {ondatechanged (null, 0, 0, 0);} public void ondatechanged (DatePicker view, int year, int monthofyear,int dayofmonth) {//Get calendar instance Calendars calendar = Calenda R.getinstance (); Calendar.set (Datepicker.getyear (), Datepicker.getmonth (), Datepicker.getdayofmonth (), Timepicker.getcurrenthour (), Timepicker.getcurrentminute ()); SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD Day hh:mm");d Atetime = Sdf.format (Calendar.gettime ()); Ad.settitle (dateTime);} /** * Implementation of the initial datetime July 02, 2012 16:45 split into months and seconds, and assigned to the calendar * * @param initdatetime * Initial datetime value String type * @return Calendar */private Calendar getcalendarbyinintdata (String initdatetime) {Calendar calendar = calendar.getinstance ();// The initial date Time July 02, 2012 16:45 split into months and days seconds string date = Splitestring (Initdatetime, "Day", "index", "front"); Date string time = Splitestring (Initdatetime, "Day", "index", "back"); Time string yearstr = splitestring (date, "year", "inDex "," front "); Year string monthandday = splitestring (date, "year", "Index", "back"); Month Day String monthstr = Splitestring (Monthandday, "month", "index", "front"); Month string daystr = splitestring (Monthandday, "month", "index", "back"); Day string hourstr = splitestring (Time, ":", "Index", "front"); When string minutestr = Splitestring (Time, ":", "Index", "back"); divide int currentyear = integer.valueof (Yearstr.trim ()). Intvalue (); int currentmonth = Integer.valueof (Monthstr.trim ()). Intvalue ()-1;int currentday = integer.valueof (Daystr.trim ()). Intvalue (); int currenthour = Integer.valueof ( Hourstr.trim ()). Intvalue (); int currentminute = Integer.valueof (Minutestr.trim ()). Intvalue (); Calendar.set ( CurrentYear, Currentmonth, Currentday, currenthour,currentminute); return calendar;}  /** * Intercept substring * * @param srcstr * Source String * @param pattern * Match mode * @param indexorlast * @param frontorback * @return */public static string splitestring (String srcstr, String pattern,string indexorlast, String frontoRback) {String result = ""; int loc = -1;if (indexorlast.equalsignorecase ("index")) {loc = Srcstr.indexof (pattern); Gets the position of the first occurrence of the string} else {loc = Srcstr.lastindexof (pattern);//The position of the last matched string}if (Frontorback.equalsignorecase ("front")) {if (loc! =-1) result = srcstr.substring (0, loc); Intercept substring} else {if (loc! =-1) result = srcstr.substring (loc + 1, srcstr.length ());//intercept substring}return result;}}

/datetimepicker/src/com/wwj/datetimepicker/datetimepickeractivity.java
Package Com.wwj.datetimepicker;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import android.widget.edittext;/** * Time Picker interface * * @author wwj_748 * */public CLA SS Datetimepickeractivity extends activity {/** Called when the activity is first created. */private EditText Startdatetim E;private EditText enddatetime;private String initstartdatetime = "September 3, 2013 14:44"; Initialization start time private String initenddatetime = "August 23, 2014 17:44"; Initialization end time @overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);//two input boxes StartDateTime = (EditText) Findviewbyid (r.id.inputdate); enddatetime = ( EditText) Findviewbyid (R.ID.INPUTDATE2); Startdatetime.settext (initstartdatetime); Enddatetime.settext ( Initenddatetime); Startdatetime.setonclicklistener (new Onclicklistener () {public void OnClick (View v) { Datetimepickdialogutil Datetimepickdialog = new Datetimepickdialogutil (DatetimepickeRactivity.this, Initenddatetime);d Atetimepickdialog.datetimepickdialog (StartDateTime);}); Enddatetime.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {Datetimepickdialogutil Datetimepickdialog = new Datetimepickdialogutil (datetimepickeractivity.this, initenddatetime); Datetimepickdialog.datetimepickdialog (EndDateTime);}});}}

Custom interface:/datetimepicker/res/layout/common_datetime.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <datepicker        android:id= "@+id/datepicker" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "        android:calendarviewshown=" false "/>    < Timepicker        android:id= "@+id/timepicker"        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"/></linearlayout>


source Download: http://download.csdn.net/detail/wwj_748/7806993

Android Custom Datatimepicker (date picker)

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.