Use of date and time controls in Android

Source: Internet
Author: User

This article describes how to use the date and time controls in Android. A demo is used to show how to use the date and time controls:

You can see that the demo displays the date or time dialog box by clicking select date and select time, and then sets the date or time, after the setting is complete, the set date or time value is displayed in the text box.

[1] demo program framework diagram:

[2] layout file Res/layout/Main. xml source code:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: gravity = "center" Android: text = "Welcome to Andy. chen blog "/> <textview Android: layout_width =" fill_parent "Android: layout_height =" wrap_content "Android: gravity =" center "Android: TEXT = "demo of date and time controls"/> <linearlayout Android: Orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <edittext Android: Id = "@ + ID/showdate" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_weight = "1"/> <button Android: Id = "@ + ID/pickdate" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "select date"/> </linearlayout> <linearlayout Android: Orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <edittext Android: id = "@ + ID/Showtime" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_weight = "1"/> <button Android: id = "@ + ID/picktime" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "Select time"/> </linearlayout>

[3] mainactivity. Java source code under com. andyidea. calenderdemo:

Package COM. andyidea. calenderdemo; import Java. util. calendar; import android. app. activity; import android. app. datepickerdialog; import android. app. dialog; import android. app. timepickerdialog; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. widget. button; import android. widget. datepicker; import android. widget. edittext; import android. widge T. timepicker; public class mainactivity extends activity {private edittext showdate = NULL; private button pickdate = NULL; private edittext Showtime = NULL; private button picktime = NULL; Private Static final int show_datapick = 0; private Static final int date_dialog_id = 1; Private Static final int show_timepick = 2; Private Static final int time_dialog_id = 3; private int myear; private int mmonth; Private int mday; private int mhour; private int mminute;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); initializeviews (); final calendar c = calendar. getinstance (); myear = C. get (calendar. year); mmonth = C. get (calendar. month); mday = C. get (calendar. day_of_month); MH Our = C. get (calendar. hour_of_day); mminute = C. get (calendar. minute); setdatetime (); settimeofday ();}/*** initialize the control and UI view */private void initializeviews () {showdate = (edittext) findviewbyid (R. id. showdate); pickdate = (button) findviewbyid (R. id. pickdate); Showtime = (edittext) findviewbyid (R. id. showtime); picktime = (button) findviewbyid (R. id. picktime); pickdate. setonclicklistener (new view. onclicklist Ener () {@ overridepublic void onclick (view v) {message MSG = new message (); If (pickdate. equals (button) V) {MSG. what = mainactivity. show_datapick;} mainactivity. this. dateandtimehandler. sendmessage (MSG) ;}}); picktime. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {message MSG = new message (); If (picktime. equals (button) V) {MSG. what = mainactivity. show _ Timepick;} mainactivity. this. dateandtimehandler. sendmessage (MSG) ;}}) ;}/ *** set date */private void setdatetime () {final calendar c = calendar. getinstance (); myear = C. get (calendar. year); mmonth = C. get (calendar. month); mday = C. get (calendar. day_of_month); updatedatedisplay ();}/*** update date display */private void updatedatedisplay () {showdate. settext (New stringbuilder (). append (myear ). append ("-"). append (( Mmonth + 1) <10? "0" + (mmonth + 1): (mmonth + 1). append ("-"). append (mday <10 )? "0" + mday: mday);}/*** date control event */private datepickerdialog. ondatesetlistener mdatesetlistener = new datepickerdialog. minute () {public void ondateset (datepicker view, int year, int monthofyear, int dayofmonth) {myear = year; mmonth = monthofyear; mday = dayofmonth; updatedatedisplay ();}}; /*** set time */private void settimeofday () {final calendar c = calendar. getinstance (); mhour = C. get (calendar. hour_of_day); mminute = C. get (calendar. minute); updatetimedisplay ();}/*** Update Time Display */private void updatetimedisplay () {showtime. settext (New stringbuilder (). append (mhour ). append (":"). append (mminute <10 )? "0" + mminute: mminute);}/*** time control event */private timepickerdialog. ontimesetlistener mtimesetlistener = new timepickerdialog. ontimesetlistener () {@ overridepublic void ontimeset (timepicker view, int hourofday, int minute) {mhour = hourofday; mminute = minute; updatetimedisplay ();}}; @ override protected dialog oncreatedialog (int id) {Switch (ID) {Case date_dialog_id: return New datepickerdialog (this, mdatesetlistener, myear, mmonth, mday); Case time_dialog_id: return new timepickerdialog (this, mtimesetlistener, mhour, mminute, true);} return NULL ;}@ override protected void onpreparedialog (int id, dialog DIALOG) {Switch (ID) {Case date_dialog_id: (datepickerdialog) DIALOG ). updatedate (myear, mmonth, mday); break; Case time_dialog_id: (timepickerdialog) DIALOG ). updatetime (mhour, mminute); break ;}/ *** handler that processes the date and time controls */handler dateandtimehandler = new handler () {@ override public void handlemessage (Message MSG) {Switch (MSG. what) {Case mainactivity. show_datapick: showdialog (date_dialog_id); break; Case mainactivity. show_timepick: showdialog (time_dialog_id); break ;}}};}

[4] program running:



Related Article

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.