In Android, the DatePicker and Timepicker components are date selectors and time selectors, respectively.
In this instance, when the date selector button is clicked, the Date selection box pops up and the user selects the date after the andoid will display the date to the TextView component; When the time selector button is clicked, the pop-up Time selection box is selected after the corresponding TextView component displays the time.
Date selection box and time selection box in the process of creating an instance, the parameters of the constructor method are similar, as described below:
1. Create an instance of the date selection box as follows
Datepickerdialog datepicker=new Datepickerdialog (context context, Ondatesetlistener CallBack, int year, int monthofyear , int dayofmonth);
The second parameter is the date selection box's listening method, through which you can return the date of the selection, the next three parameters are the initial date display of the pop-up date selection box.
The date selection box is monitored as follows, and the Ondateset () method needs to be covered.
Datepickerdialog.ondatesetlistener date_callback=new Datepickerdialog.ondatesetlistener () {@Override public void Ondateset (DatePicker arg0, int arg1, int arg2, int arg3) {//arg1~arg3 is the chosen date textview_date.settext (stri Ng.format ("%s year%s month%s Day", Format_conver (Arg1), Format_conver (arg2), Format_conver (ARG3)));//Custom One format_conver () Method guaranteed format Output}};
2. The time selection box creates an instance and the Date selection box procedure is similar, the parameters are basically the same.
Timepickerdialog timepicker=new Timepickerdialog (context context, Ontimesetlistener callBack, int hourofday, int minute , Boolean Is24hourview);
The last parameter is whether the time is 12-hour or 24-hour display.
The time selection box is monitored as follows, and the Ontimeset () method needs to be rewritten.
Timepickerdialog.ontimesetlistener time_callback=new Timepickerdialog.ontimesetlistener () {@Override public void Ontimeset (Timepicker arg0, int arg1, int arg2) {//arg1 represents hours, arg2 represents minutes Textview_time.settext (string.form at ("%s:%s", Format_conver (Arg1), Format_conver (ARG2)));//Format output}};
The example test code is as follows:
The first is the layout file:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:id= "@+id/linearlayout1" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:orientation= "vertical" tools:context= "main.test_dt_ Picker. Mainactivity "> <TextView Android:id= "@+id/tv1_date" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:textsize= "30SP" android:text= "@ String/tv_date "/> <button android: Id= "@+id/btn1_date" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/btn_date" /> <TextView android:id= "@+id/tv2_time" android: Layout_width= "Wrap_content" android:layout_height= "wrap_content " android:textsize=" 30SP " android:text= "@string/tv_time"/> <button android:id= "@+id/btn2_time" Android:layout_width= "wrap_content" android:layout_height= "wrap _content " android:text= "@string/btn_time"/></linearlayout>
Next is the Strings.xml file:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >test_dt_picker</ string> <string name= "action_settings" >Settings</string> <string name= "Tv_date" >none</ string> <string name= "Tv_time" >None</string> <string name= "btn_date" > Date selector </string> &l T;string name= "Btn_time" > Time picker </string></resources>
Again is the Android source file:
package main.test_dt_picker;import android.app.datepickerdialog;import android.app.timepickerdialog;import android.os.bundle;import android.support.v7.app.actionbaractivity;import android.view.view;import android.view.view.onclicklistener;import android.widget.button;import android.widget.datepicker; Import android.widget.textview;import android.widget.timepicker;public class mainactivity extends actionbaractivity implements onclicklistener{ private Button button_Date=null,button_Time=null; private TextView Textview_date=null,textview_time=null; private datepickerdialog datepicker= null; private timepickerdialog timepicker=null; @Override protected void oncreate (bundle Savedinstancestate) &NBSp { super.oncreate (savedinstancestate); setcontentview (R.layout.activity_main); button_date= (Button) Findviewbyid (R.id.btn1_Date); button_time= (Button) Findviewbyid (r.id.btn2_time); textview_date= (TextView) Findviewbyid (r.id.tv1_date); textview_time= (TextView) Findviewbyid (r.id.tv2_time); button_date.setonclicklistener ( Mainactivity.this); button_time.setonclicklistener ( Mainactivity.this); datepicker=new datepickerdialog (mainactIVITY.THIS,&NBSP;DATE_CALLBACK,&NBSP;2012,&NBSP;6,&NBSP;18); timepicker=new timepickerdialog (MainActivity.this, Time_callback, 10, 5, true); } @Override public void onclick (view arg0) { // TODO Auto-generated method stub int btn_id=arg0.getid (); switch ( btn_id) { case r.id.btn1_date: datepicker.show () ; Break; case r.id.btn2_time: timepicker.show (); break; default: break; } } datepickerdialog.ondatesetlistener date_ Callback=new datepickerdialog.ondatesetlistener () { @Override Public void ondateset (datepicker arg0, int arg1, int arg2, int ARG3) { textview_date.settext (String.Format ("%s year%s month%s Day", Format_conver ( ARG1), Format_conver (arg2), Format_conver (ARG3))); } }; TimePickerDialog.OnTimeSetListener time_callback=new Timepickerdialog.ontimesetlistener () { @Override public void ontimeset (timepicker arg0, int ARG1,&NBSP;INT&NBSP;ARG2) { textview_time.settext (String.format ("%s:%s", format_conver (Arg1), Format_conver (arg2))); } }; public string format_conver (int s) {//The method is guaranteed to be preceded by a 0 in order to output one digit. To align it with the implementation of the 10-digit number, such as the time is 12:5, use this method after the output is 12:05 return s>=10? "" +s: "0" +S;&NBSP;&NBSP;&NBSP;&NBSP;}}
Finally, there are three test photos:
The first post-boot original image:
Click the Date Selection box button to pop up the date selection box:
Final Test Picture:
DatePicker and Timepicker of common Android components