Timepickerdialog (Time selection dialog box)
To create a Timepickerdialog Time selection dialog box:
1. Create a class inheritance Dialogfragement
2. Override the Oncreatedialog () method to return a Timepickerdialog object
3. Implement the Timepickerdialog Ontimesetlistener interface to receive a callback when the user sets the time
Datepickerdialog (Date selection dialog box)
To create a Datepickerdialog Date selection dialog box:
1. Create a class inheritance Dialogfragment
2. Override the Oncreatedialog () method to return a Timepickerdialog object
3. Implement the Datepickerdialog Ondatesetlistener interface to receive a callback when the user sets the date
Datepickerdialog and Timepickerdialog are Android-provided dialog boxes that pop up a date, time selection, We can get a date, Time dialog box by instantiating Datepickerdialog and Timepickerdialog in the program. These two classes are alertdialog subclasses of the
I have provided a Baidu official note about picker, which may be helpful in understanding this article: Http://wenku.baidu.com/link?url= U77tpsl99lpf8qrgd2h6xka6xjqzk-jsecnjhc9vmgyowckhcz8-3f3tzokozglverjwt6jitlhw-wwmodaoqltepvxryazxnlcqstojlck
Click to open link
Source:
Html
<linearlayout 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:orie ntation= "vertical" > <button android:id= "@+id/button1_settime" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Set Time" android:onclick= "Settimeclick"/> & Lt TextView android:id= "@+id/tv_settime" android:layout_width= "wrap_content" android:layout_height= "Wra P_content "/> <button android:id=" @+id/button1_setdate "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "Set Date" android:onclick= "Setdateclick"/> <Text View android:id= "@+id/tv_setdate" android:layout_width= "wrap_content" android:layout_height= "Wrap_co Ntent "/> </LinearLayout>
Java
<span style= "FONT-SIZE:12PX;" >import Java.util.calendar;import Android.os.bundle;import Android.annotation.suppresslint;import Android.app.activity;import Android.app.datepickerdialog;import Android.app.dialog;import Android.app.dialogfragment;import Android.app.timepickerdialog;import Android.text.format.dateformat;import Android.view.menu;import Android.view.view;import Android.widget.button;import Android.widget.DatePicker;import Android.widget.textview;import Android.widget.TimePicker; @SuppressLint ({"Newapi", "validfragment"}) public class Mainactivity extends Activity {private Button btnsettime;private TextView tv_settime;private TextView tv_setdate; protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Instantiate an object, bind btnsettime = (Button) Findviewbyid (r.id.button1_settime) through Findviewbyid and the corresponding XML layout component; Tv_settim E = (TextView) Findviewbyid (r.id.tv_settime); tv_setdate = (TextView) Findviewbyid (r.id.tv_setdate);} Set Date event handling method public void Setdateclick (View v) {Setdatedialog SDT = new Setdatedialog (); Sdt.show (Getfragmentmanager (), " DatePicker ");} Set the time event handling method public void Settimeclick (View v) {Settimedialog std = new Settimedialog (); Std.show (Getfragmentmanager (), " Timepicker ");} public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the action bar if it is present.get Menuinflater (). Inflate (R.menu.main, menu); return true;} Create Date selection dialog box class Setdatedialog extends Dialogfragment implements Datepickerdialog.ondatesetlistener{public Dialog Oncreatedialog (Bundle savedinstancestate) {//calendar is an abstract class that is obtained by getinstance (), set to system default time final Calendar C = Ca Lendar.getinstance ();//Get year, month, day int = C.get (calendar.year); int month = C.get (calendar.month); in T day= c.get (calendar.day_of_month); datepickerdialog DPD = new Datepickerdialog (getactivity (), this, year, month, day); RetuRN DPD;} public void Ondateset (DatePicker view, int years, int month, int day) {Tv_setdate.settext (year+ "Years" + (month+1) + "month" +day+ "Day") ;}} Create Time Selection dialog box @suppresslint ("Newapi") class Settimedialog extends Dialogfragment implements timepickerdialog.ontimesetlistener{@SuppressLint ("Newapi") public Dialog Oncreatedialog (Bundle savedinstancestate) {//Get hours, minutes final Calendar C = calendar.getinstance (); int hour = C.get (calendar.hour_of_day); int min Ute = C.get (Calendar.minute); Timepickerdialog TPD = new Timepickerdialog (getactivity (), This,hour, Minute, Dateformat.is24hourformat (GetActivity () ); return TPD;} public void Ontimeset (timepicker view, int hourofday, int minute) {Tv_settime.settext (hourofday+ ":" +minute);}}} </span>
Effect Show:
Android Note--date&time (Date Time selection dialog box)