1. DatePicker inherits from the FrameLayout class. The date Selection Control provides users with date data including year, month, and day, and allows users to modify the data. To capture Data Events in the user's date Selection control, you need to add an OnDateChangedListener listener for DatePicker.
Ii. 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 controls
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 = (DatePicker) findViewById (R. id. datePicker );
TimePicker timePicker = (TimePicker) findViewById (R. id. timePicker );
Calendar 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