Use Timepicker and DatePicker for time and date in Android

Source: Internet
Author: User

First introduce the use of Timepicker

    1. 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

    1. The code portion of the XML file



    2. <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

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.