Introduction
The following uses an example to show the functions and usage of datepicker and timepicker.
Step 1: Create a project dateandtimepicker
Step 2: Compile the app UI/layout/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 = "select the time to purchase this 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: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: editable = "false" Android: cursorvisible = "false" Android: id = "@ + ID/Show"/> </linearlayout>
Step 3: main program mainactivity. Java
Package CN. roco. picker; import Java. util. calendar; import android. app. activity; import android. OS. bundle; import android. widget. datepicker; import android. widget. datepicker. ondatechangedlistener; import android. widget. edittext; import android. widget. timepicker; import android. widget. timepicker. ontimechangedlistener; public class mainactivity extends activity {private int year; private int month; private int day; private int hour; private int minute;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); datepicker = (datepicker) findviewbyid (R. id. datepicker); timepicker = (timepicker) findviewbyid (R. id. timepicker); Calendar calendar = calendar. getinstance (); year = calendar. get (calendar. year); month = calendar. get (calendar. month); Day = calendar. get (calendar. day_of_month); hour = calendar. get (calendar. hour); minute = calendar. get (calendar. minute); datepicker. init (year, month, day, new ondatechangedlistener () {@ overridepublic void ondatechanged (datepicker view, int year, int month, int day) {mainactivity. this. year = year; mainactivity. this. month = month; mainactivity. this. day = day; // display time showdate (year, month, day, hour, minute) ;}}); timepicker. setontimechangedlistener (New ontimechangedlistener () {@ overridepublic void ontimechanged (timepicker view, int hour, int minute) {mainactivity. this. hour = hour; mainactivity. this. minute = minute; // display time showdate (year, month, day, hour, minute) ;}}) ;}private void showdate (INT year, int month, int day, int hour, int minute) {edittext show = (edittext) findviewbyid (R. id. show); show. settext ("your purchase date is: "+ year +" year "+ month +" month "+ day +" day "+ hour +" Hour "+ minute +" Minute ");}}
Step 4: deploy the application to the simulator and select the date and time