Code
Packagecom.lxt008;ImportJava.util.Calendar;ImportCOM.LXT008.R;Importandroid.app.Activity;ImportAndroid.app.DatePickerDialog;ImportAndroid.app.TimePickerDialog;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.DatePicker;ImportAndroid.widget.TextView;ImportAndroid.widget.TimePicker; Public classActivity01extendsactivity{TextView M_textview; //declaring DatePicker objectsDatePicker M_datepicker; //declaring Timepicker objectsTimepicker M_timepicker; Button M_dpbutton; Button M_tpbutton; //Calendar Classes in JavaCalendar C; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); C=calendar.getinstance (); M_textview=(TextView) Findviewbyid (R.ID.TEXTVIEW01); M_dpbutton=(Button) Findviewbyid (R.id.button1); M_tpbutton=(Button) Findviewbyid (R.id.button2); //get DatePicker ObjectM_datepicker =(DatePicker) Findviewbyid (R.ID.DATEPICKER01); //initializes the calendar to the current system time and sets its event listenerM_datepicker.init (C.get (calendar.year), C.get (Calendar.month), C.get (Calendar.day_of_month),NewDatepicker.ondatechangedlistener () {@Override Public voidOndatechanged (DatePicker view,intYearintMonthofyear,intdayofmonth) { //when the date changes, this is handled here//C.set (year, monthofyear, DayOfMonth); } }); //Get Timepicker ObjectM_timepicker =(Timepicker) Findviewbyid (R.ID.TIMEPICKER01); //set to 24-hour displayM_timepicker.setis24hourview (true); //Monitor Time ChangeM_timepicker.setontimechangedlistener (NewTimepicker.ontimechangedlistener () {@Override Public voidOntimechanged (Timepicker view,intHourofday,intminute) { //processing when time changes//C.set (year, month, day, Hourofday, minute, second); } }); M_dpbutton.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {NewDatepickerdialog (Activity01. This, NewDatepickerdialog.ondatesetlistener () { Public voidOndateset (DatePicker view,intYearintMonthofyear,intdayofmonth) { //set up a calendar}},c.get (Calendar.year), C.get (Calendar.month), C.get (Calendar.day_of_mont H). Show (); } }); M_tpbutton.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {NewTimepickerdialog (Activity01. This, NewTimepickerdialog.ontimesetlistener () { Public voidOntimeset (Timepicker view,intHourofday,intminute) { //Set the time}},c.get (Calendar.hour_of_day), C.get (Calendar.minute),true). Show (); } }); }}
Layout file
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" ><TextViewAndroid:id= "@+id/textview01"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello" /> <DatePickerAndroid:id= "@+id/datepicker01"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" > </DatePicker> <TimepickerAndroid:id= "@+id/timepicker01"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" > </Timepicker> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Set Date" > </Button> <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Set Time" > </Button></LinearLayout>
Android-timepicker/datepicker Time Picker Demo