Datepicker/timerpicker/calendar/datepickerdialog/timepickerdialog

Source: Internet
Author: User

First, let's talk about calendar. calendar is not something of Android, but something in Java, a very powerful class about time operations in Java. I will not go into details here. There are too many methods and skills to use. Here we will focus on the controls in Android.

----------------------------------------------------------------------------

Datepicker: Date selector.

Code Application Example:

 

 Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. datepicker; <br/> Import android. widget. toast; <br/> Import android. widget. datepicker. ondatechangedlistener; </P> <p> public class datepickeractivity extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate ); <Br/> setcontentview (R. layout. datepicker); <br/> datepicker = (datepicker) This. findviewbyid (R. id. datepicker1); <br/>/* initialization time, INIT (INT year, int monthofyear, int dayofmonth, ondatechangedlistener) <br/> * year indicates the year <br/> * monthofyear indicates the month. Note that the month starts from 0. For example, if we specify March, we need to pass in 2 <br/> * the day of the month in dayofmonth <br/> * ondatechangedlistener. After we modify the time, listener <br/> **/<br/> datepicker. init (2011, 3, 11, new datepicker. ondatechangedlistener () {</P> <p>/** <br/> * when the time is modified, method of execution <br/> */<br/> @ override <br/> Public void ondatechanged (datepicker view, int year, int monthofyear, <br/> int dayofmonth) {<br/> toast. maketext (datepickeractivity. this, "year =" + year + "monthofyear =" + monthofyear, toast. length_long ). show (); <br/>}< br/>}); </P> <p >}< br/>

Pattern:

Here we can see that we have entered the current window, and the date Selection control is displayed. In many cases, we do not want to select a date. We hope that, you can click a button or image to bring up a dialog box with our date selector. At this time, we need to use another control, datepickerdialog. Datepickerdialog is a datepicker loaded in the dialog box. I will not talk about it here. The usage of the two is basically the same.

-------------------------------------------------------------------------

Timepicker: Time Selector

Sample Code:

Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. timepicker; <br/> Import android. widget. toast; </P> <p> public class timepickeractivity extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> // todo auto-generated method stub <br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. timepicker); <br/> timepicker = (timepicker) findviewbyid (R. id. timepicker1); <br/> timepicker. setis24hourview (true); <br/> timepicker. setcurrenthour (12); <br/> timepicker. setcurrentminute (23); <br/> // timepicker. setvisibility (view. visible); </P> <p> timepicker. setontimechangedlistener (New timepicker. ontimechangedlistener () {</P> <p> @ override <br/> Public void ontimechanged (timepicker view, int hourofday, int minute) {</P> <p> toast. maketext (timepickeractivity. this, "Change Time To hour:" + hourofday + "minute:" + minute, toast. length_long ). show (); <br/>}< br/>}); <br/>}</P> <p>}

Layout file example:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "match_parent"> <br/> <timepicker Android: Id = "@ + ID/timepicker1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </timepicker> <br/> </linearlayout> <br/>

 

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.