Let's take a look:
Post the followingCodeHere, the main. XML code is:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: id = "@ + ID/textview1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "select the specific time to purchase the book"/> <datepicker Android: Id = "@ + ID/datepicker" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_horizontal"/> <timepicker Android: Id = "@ + ID/timepicker" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_horizontal"/> <edittext Android: Id = "@ + ID/Show" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: cursorvisible = "false" Android: editable = "false"/> </linearlayout>
The Java code is:
Public class androiddemo5activity extends activity {// record the current time private int year; private int month; private int day; private int hour; private int minute; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. main); datepicker date = (datepicker) findviewbyid (R. id. datepicker); timepicker time = (timepicker) findviewbyid (R. id. timepicker); // The current year, month, day, hour, and minute calendar CA = calendar. getinstance (); year = Ca. get (calendar. year); month = Ca. get (calendar. month); Day = Ca. get (calendar. day_of_month); hour = Ca. get (calendar. hour); minute = Ca. get (calendar. minute); // initialize datepickerdate. init (year, month, day, new ondatechangedlistener () {@ overridepublic void ondatechanged (datepicker arg0, int year, int month, int day) {androiddemo5activity. this. year = year; androiddemo5activity. this. month = month; androiddemo5activity. this. day = day; // display the current time and date showdate (year, month, day, hour, minute) ;}}); // specify the event listener time for timerpicker. setontimechangedlistener (New ontimechangedlistener () {@ overridepublic void ontimechanged (timepicker arg0, int hour, int minute) {androiddemo5activity. this. hour = hour; androiddemo5activity. this. minute = minute ;}}) ;}protected void showdate (INT year2, int mon2, int day2, int hour2, int minute2) {edittext text = (edittext) findviewbyid (R. id. show); text. settext ("your purchase time is: "+ year2 +" year "+ mon2+" month "+ day2 +" day "+ hour2 +" Hour "+ minute2 +" Minute ");}}