Main. xml
<? 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: text = "date control Demo"/> <linearlayout Android: Orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <edittext Android: Id = "@ + ID/showdate" Android: layout_width = "200dip" Android: layout_height = "wrap_content"/> <button Android: Id = "@ + ID/but_showdate" Android: layout_width = "100dip" Android: layout_height = "wrap_content" Android: TEXT = "select date"/> </linearlayout>
Mainactivity. Java source code:
Package COM. wanghf. demo; import Java. util. calendar; import android. app. activity; import android. app. datepickerdialog; import android. app. dialog; 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;/*** simple use of the date control ** @ author adminelco * @ time 2011-07- 20 15:42:24 **/public class mainactivity extends activity {private edittext showdate = NULL; private button pickdate = NULL; Private Static final int date_dialog_id = 1; Private Static final int show_datapick = 0; private int myear; private int mmonth; private int mday;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (S Avedinstancestate); setcontentview (R. layout. main); showdate = (edittext) findviewbyid (R. id. showdate); pickdate = (button) findviewbyid (R. id. but_showdate); pickdate. setonclicklistener (New datebuttononclicklistener (); final calendar c = calendar. getinstance (); myear = C. get (calendar. year); mmonth = C. get (calendar. month); mday = C. get (calendar. day_of_month); setdatetime ();} 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); updatedisplay ();}/*** update date */private void updatedisplay () {showdate. settext (New stringbuilder (). append (myear ). append (mmonth + 1) <10? "0" + (mmonth + 1): (mmonth + 1). append (mday <10 )? "0" + mday: mday);}/*** date control event */private datepickerdialog. ondatesetlistener mdatesetlistener = new datepickerdialog. ondatesetlistener () {public void ondateset (datepicker view, int year, int monthofyear, int dayofmonth) {myear = year; mmonth = monthofyear; mday = dayofmonth; updatedisplay ();}}; /*** select date button event processing ** @ author Raul **/class datebuttononclicklistener implements android. view. view. onclicklistener {@ override public void onclick (view v) {message MSG = new message (); If (pickdate. equals (button) V) {MSG. what = mainactivity. show_datapick;} mainactivity. this. salehandler. sendmessage (MSG) ;}@ override protected dited oncreatedialog (int id) {Switch (ID) {Case date_dialog_id: return New datepickerdialog (this, mdatesetlistener, myear, mmonth, mday) ;}return null ;}@ override protected void onpreparedialog (int id, dialog DIALOG) {Switch (ID) {Case date_dialog_id: (datepickerdialog) DIALOG ). updatedate (myear, mmonth, mday); break ;}/ *** handler that processes the date control */handler salehandler = new handler () {@ override public void handlemessage (Message MSG) {Switch (MSG. what) {Case mainactivity. show_datapick: showdialog (date_dialog_id); break ;}}};}
Source: http://blog.csdn.net/android_home/article/details/6620206