DatePicker control and TimePicker control developed for Android
The DatePicker control 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.
The TimePicker control inherits from the FrameLayout class and displays the time of the day to the user, allowing the user to modify it.
DatePicker control method:
1. init ()
Initialize the DatePicker date Selection control, set the year, month, and number of days for initial display, and set the date change listener. Here, the monthOfYear parameter is the initial month, and the actual displayed month is the value set here plus 1.
2. OnDateChangedListener ()
Set the date listener.
3. updateDate
Update the DatePicker control. Calling this method triggers the date change listener.
The TimePicker control method is as follows:
1. setCurrentHour ()
Set the hour for TimePicker display.
2. setCurrentMinute ()
Set the display time of TimePicker.
3. setIs24HourView ()
The time option controls are displayed in 24 hours.
4. setOnTimeChangedListener ()
Set the time to change the listener.
Small instance:
MainActivity. java
Public class MainActivity extends Activity {DatePicker datePicker; TimePicker timePicker; TextView timeStr, dateStr; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); datePicker = (DatePicker) findViewById (R. id. date); timePicker = (TimePicker) findViewById (R. id. time); timeStr = (TextView) findViewById (R. id. time_str); dateStr = (TextView) findViewById (R. id. date_str); Calendar calendar = Calendar. getInstance (); int year = calendar. get (Calendar. YEAR); int monthOfYear = calendar. get (Calendar. MONTH); int dayOfMonth = calendar ar. get (Calendar. DAY_OF_MONTH); datePicker. init (year, monthOfYear, dayOfMonth, new OnDateChangedListener () {@ Overridepublic void onDateChanged (DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {dateStr. setText (year + "year" + (monthOfYear + 1) + "month" + dayOfMonth + "day") ;}}); timePicker. setOnTimeChangedListener (new OnTimeChangedListener () {@ Overridepublic void onTimeChanged (TimePicker arg0, int hour, int minuter) {timeStr. setText (hour + "" + minuter + "");}});}}
Activity_main.xml
: