Timepicker also inherits from the framelayout class. The Time Selection Control displays the time of a day (in 24 hours or am/pm format) to the user and allows the user to select. To capture the event of the time data modified by the user, you need to add the ontimechangedlistener listener for the timepicker.
Usage of the following Date and Time Selection Control: directory structure main. xml layout File
<?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">
<datepicker android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<edittext android:id="@+id/dateet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorvisible="false"
android:editable="false"/>
<timepicker android:id="@+id/timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<edittext android:id="@+id/timeet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorvisible="false"
android:editable="false"/>
</linearlayout>
Dptpactivity class
Package com. ljq. activity;
Import java. util. calendar;
Import android. app. activity;
Import android. OS. bundle;
Import android. widget. datepicker;
Import android. widget. edittext;
Import android. widget. timepicker;
Import android. widget. datepicker. ondatechangedlistener;
Import android. widget. timepicker. ontimechangedlistener;
Public class dptpactivity extends activity {
Private edittext dateet = null;
Private edittext timeet = null;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (r. layout. main );
Dateet = (edittext) findviewbyid (r. id. dateet );
Timeet = (edittext) findviewbyid (r. id. timeet );
Datepicker = (datepicker) findviewbyid (r. id. datepicker );
Timepicker = (timepicker) findviewbyid (r. id. timepicker );
Calendar = calendar. getinstance ();
Int year = calendar. get (calendar. year );
Int monthofyear = calendar. get (calendar. month );
Int dayofmonth = calendar. get (calendar. day_of_month );
Datepicker. init (year, monthofyear, dayofmonth, new ondatechangedlistener (){
Public void ondatechanged (datepicker view, int year,
Int monthofyear, int dayofmonth ){
Dateet. settext ("the date you selected is:" + year + "year" + (monthofyear + 1) + "month" + dayofmonth + "day. ");
}
});
Timepicker. setontimechangedlistener (new ontimechangedlistener (){
Public void ontimechanged (timepicker view, int hourofday, int minute ){
Timeet. settext ("the time you selected is:" + hourofday + "Hour" + minute +. ");
}
});
}
}
Running result