First introduce the use of Timepicker
XML File Code section
<timepicker android:id= "@+id/firsttime1" android:layout_width= "wrap_content" android:layout_height= "Wrap_cont Ent "/><button android:id=" @+id/button "android:layout_width=" fill_parent "android:layout_height=" Wrap_ Content "android:layout_below=" @id/firsttime1 "android:text=" gets the value of Timepicker "/>
2.MainActivity part
private timepicker firsttimepicker;private button button; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview (R.layout.activity_main); Firsttimepicker= (Timepicker) Findviewbyid (r.id.firsttime1); Firsttimepicker.setis24hourview (TRUE);//is set to 24-hour Firsttimepicker.setcurrenthour (12);//Set the display time, the default is to display the current time Firsttimepicker.setcurrentminute ( timelist timel=new); timelist (); //firsttimepicker.setontimechangedlistener ( Timel); button= (Button) Findviewbyid (R.id.button); buttonlis bl= New buttonlis (); button.setonclicklistener (BL);// Use the buttons below to toast time } class ButtonLis implements onclicklistener{@Overridepublic void onclick (view v) {int hour= Firsttimepicker.getcurrenthour (); Int min=firsttimepicker.getcurrentminute (); Toast.maketext (Getapplicationcontext (), hour+ ":" +min, toast.length_short). Show (); } class timelist implements ontimechangedlistener{ //view TIMEPICKER&NBSP;&NBSP;&NBSP;&NBSP;//HOUROFDAY&NBSP that the object represents;: Hours @Overridepublic selected by the user void ontimechanged (Timepicker view, int hourofday, int minute) {toast.maketext (Getapplicationcontext (), hourofday+ "-" +minute, toast.length_long). Show (); }
Next introduce the use of DatePicker
The code portion of the XML file
<datepicker android:id= "@+id/datepick" android:layout_width= "wrap_content" android:layout_height= "Wrap_conten T "/><button android:id=" @+id/but "android:layout_width=" fill_parent "android:layout_height=" Wrap_c Ontent "android:layout_below=" @id/datepick "android:text=" Get DatePicker value "/>
Mainactivity part
private datepicker date;private button but; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); date= (DatePicker) Findviewbyid (R.id.datepick); but= (Button) findViewById (R.id.but) ; date.updatedate (2013, 6, 6);//Set a display time, otherwise the current system time butlis butlis=new butlis (); but.setonclicklistener (Butlis); }class butlis implements onclicklistener{@Overridepublic void onclick (view v) {int year= Date.getyear (); Int mouth=date.getmoNth (); Int day=date.getdayofmonth (); Toast.maketext (Getapplicationcontext (), "Today is" +year+ "year" +mouth+ "month" +day+ "Day" toast.length_long ". Show ();}}
This article is from the "Java White Battlefield" blog, be sure to keep this source http://8023java.blog.51cto.com/10117207/1660199
Use Timepicker and DatePicker for time and date in Android