Package COM. example. examples_04_11; import Java. util. calendar; import android. app. activity; import android. app. datepickerdialog; import android. app. timepickerdialog; import android. app. timepickerdialog. ontimesetlistener; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. datepicker; import android. widget. datepicker. ondatechangedlistener; import android. widget. textview; import android. widget. timepicker; import android. widget. timepicker. extends; public class mainactivity extends activity {private textview m_textview; private datepicker m_datepicker; private timepicker m_timepicker; private button m_dpbutton, encrypted; private calendar m_calendar; @ overrideprotected void oncreate (bundle upload) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); m_calendar = calendar. getinstance (); m_textview = (textview) findviewbyid (R. id. textview01); m_dpbutton = (button) findviewbyid (R. id. button1); m_tpbutton = (button) findviewbyid (R. id. button2); m_datepicker = (datepicker) findviewbyid (R. id. datepicker01); // initialize the calendar to the current system time, and set its event listening m_datepicker.init (m_calendar.get (calendar. year), m_calendar.get (calendar. month), m_calendar.get (calendar. day_of_month), new ondatechangedlistener () {@ overridepublic void ondatechanged (datepicker view, int year, int monthofyear, int dayofmonth) {// todo auto-generated method hour (year, monthofyear, dayofmonth) ;}}); m_timepicker = (timepicker) findviewbyid (R. id. timepicker01); m_timepicker.setis24hourview (true); // listener time change listener (New ontimechangedlistener () {@ overridepublic void ontimechanged (timepicker view, int hourofday, int minute) {// todo auto-generated method stub // m_calendar.set (year, month, day, hourofday, minute, second) ;}}); m_dpbutton.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubnew datepickerdialog (mainactivity. this, new datepickerdialog. ondatesetlistener () {@ overridepublic void ondateset (datepicker view, int year, int monthofyear, int dayofmonth) {// todo auto-generated method stub}, m_calendar.get (calendar. year), m_calendar.get (calendar. month), m_calendar.get (calendar. day_of_month )). show () ;}}); m_tpbutton.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubnew timepickerdialog (mainactivity. this, new ontimesetlistener () {@ overridepublic void ontimeset (timepicker view, int hourofday, int minute) {// set time }}, m_calendar.get (calendar. hour_of_day), m_calendar.get (calendar. minute), true ). show () ;}}) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true ;}}
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: paddingbottom = "@ dimen/activity_vertical_margin" Android: paddingleft = "@ dimen/plugin" Android: paddingright = "@ dimen/plugin" Android: paddingtop = "@ dimen/plugin" tools: context = ". mainactivity "> <textview Android: Id =" @ + ID/textview01 "Android: layout_width =" fill_parent "Android: layout_height =" wrap_content "Android: TEXT = "@ string/hello_world"/> <datepicker Android: Id = "@ + ID/datepicker01" Android: layout_below = "@ ID/textview01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <timepicker Android: Id = "@ + ID/timepicker01" Android: layout_below = "@ ID/datepicker01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <button Android: Id = "@ + ID/button1" Android: layout_below = "@ ID/timepicker01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "set date"/> <button Android: Id = "@ + ID/button2" Android: layout_below = "@ ID/button1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Set Time"/> </relativelayout>