In Android, the time and date controls are relatively rich. DatePicker is used to set the date input and TimePicker is used to set the time input. DatePickerDialog is used to display the date dialog box and TimePickerDialog is used to display the time dialog box. AnalogClock is used to display a pointer clock, while DigitalClock is used to display a digital clock.
The following is a brief introduction to these time and date controls.
1. DatePicker
In Android, DatePicker is used to set the date input. The date range is January 1.
1.1 common xml attributes
The common xml Attribute 1 of DatePicker is as follows:
Figure 1 common xml attributes of DatePicker
Android: calendarViewShown [boolean] is used to set whether to display the calendar view; android: endYear [int] is used to set the end date; android: maxDate [int] is used to set the maximum date; android: minDate [int] is used to set the minimum date. android: spinnersShown [boolean] is used to set whether to display spinners. android: startYear [int] is used to set the start date.
1.2 common methods
Commonly used DatePicker methods include:
(1) public CalendarView getCalendarView (); // obtain the CalendarView
(2) public boolean getCalendarViewShown (); // obtain whether the CalendarView is displayed
(3) public int getDayOfMonth (); // get the day of the current date
(4) public long getMaxDate (); // obtain the maximum date.
(5) public long getMinDate (); // obtain the minimum date.
(6) public int getMonth (); // obtain the month of the current date
(7) public boolean getSpinnersShown (); // you can call this operation to check whether a Spinners is displayed.
(8) public int getYear (); // get the year of the current date
(9) public void init (int year, int monthOfYear, int dayOfMonth,
DatePicker. OnDateChangedListener onDateChangedListener); // initialization date
(10) public void setCalendarViewShown (boolean shown); // you can specify whether to display the CalendarView.
(11) public void setMaxDate (long maxDate); // you can specify the maximum date.
(12) public void setMinDate (long minDate); // you can specify the minimum date.
(13) public void setSpinnersShown (boolean shown); // you can specify whether to display Spinners.
(14) public void updateDate (int year, int month, int dayOfMonth); // update the current date
2. TimePicker
In Android, TimePicker is used to set the time input. You can select 12 or 24-hour mode. Common TimePicker methods include:
(1) public Integer getCurrentHour (); // gets the hour of the current time
(2) public Integer getCurrentMinute (); // obtain the minute of the current time
(3) public boolean is24HourView (); // obtain whether the 24-hour mode is used
(4) public void setCurrentHour (Integer currentHour); // set the hour of the current time
(5) public void setCurrentMinute (Integer currentMinute); // set the minute of the current time
(6) public void setIs24HourView (Boolean is24HourView); // sets the 24-hour mode.
3. DatePickerDialog
In Android, DatePickerDialog is used to display the date dialog box. Commonly used DatePickerDialog methods include:
(1) public DatePicker getDatePicker (); // obtain the date value in DatePicker
(2) public void onClick (DialogInterface dialog, int which); // responds to the click event in the dialog box.
(3) public void onDateChanged (DatePicker view, int year, int month, int day); // responds to the date change event
(4) public void updateDate (int year, int monthOfYear, int dayOfMonth); // update the current date
4. TimePickerDialog
In Android, TimePickerDialog is used to display the time dialog box. Common TimePickerDialog methods include:
(1) public void onClick (DialogInterface dialog, int which); // responds to the click event in the dialog box.
(2) public void onTimeChanged (TimePicker view, int hourOfDay, int minute); // response time change event
(3) public void updateTime (int hourOfDay, int minuteOfHour); // update the current time
5. AnalogClock
In Android, AnalogClock is used to display the pointer clock, which only has two pointers: clock and minute.
6. DigitalClock
In Android, DigitalClock is used to display digital clocks in the format of HH: MM: ss am/PM.
7. Instance
After clarifying the attributes and methods of the preceding time and date controls, we can easily use the time and date controls.
In this example, we use DatePicker to display the Date input settings control and TimePicker to display the time input settings control. Two buttons are added to the main interface. They are used to pop up the DatePickerDialog dialog box and the TimePickerDialog dialog box. The main interface 2 is shown below:
Figure 2 Main Interface
You can use the "+" and "-" buttons in the DatePicker control to adjust the values of year, month, and day, and the "+" and "-" buttons of TimePicker to adjust the values of hour and minute respectively. To listen for changes in the value of the year, month, and day, You need to implement the android interface. widget. datePicker. onDateChangedListener onDateChanged () method; to listen for changes in the value of the hour and minute, You need to implement the interface android. widget. timePicker. onTimeChanged () method in OnTimeChangedListener. The specific implementation method is as follows:
View Code
1 /*
2 * Function: triggers a Function with a time change.
3 * Param: view time control object; hourOfDay hour; minute
4 * Author: blog Park-still indifferent
5 */
6 public void onTimeChanged (TimePicker view, int hourOfDay, int minute ){
7 Toast. makeText (MainActivity. this, "the current time is" + hourOfDay + ":" + minute,
8 Toast. LENGTH_LONG). show ();
9}
10
11 /*
12 * Function: date change trigger Function
13 * Param: view date control object; year; monthOfYear month; dayOfMonth
14 * Author: blog Park-still indifferent
15 */
16 public void onDateChanged (DatePicker view, int year, int monthOfYear, int dayOfMonth ){
17 Toast. makeText (MainActivity. this, "the current date is" + year + "year" + monthOfYear + "month" + dayOfMonth + "day ",
18 Toast. LENGTH_LONG). show ();
19}
By listening to The DatePicker control and TimePicker control, when you click the "+" or "-" button in the DatePicker control or TimePicker control, a prompt message is displayed for the current date or time, 3:
Figure 3 adjust the date value of the date Control
In addition, you can adjust the date and time values through the DatePickerDialog and TimePickerDialog in the date dialog box. Click "set date value through DatePickerDialog" to bring up the date dialog box. Click "set time value through TimePickerDialog" to bring up the time dialog box, as shown in dialog box 4.
Figure 4 Time Setting Dialog Box
In the Time Setting dialog box, you can also adjust the time value. Note that the datepickerdiener interface must be implemented in the DatePickerDialog. OnDateSetListener control, and the onDateSet () method in this interface must be implemented. In the TimePickerDialog control, you must implement the TimePickerDialog. OnTimeSetListener interface and implement the onTimeSet () method in this interface. The specific implementation method is as follows:
View Code
1 /*
2 * Function: Button response
3 * Param: v button object
4 * Author: blog Park-still indifferent
5 */
6 public void onClick (View v ){
7 switch (v. getId ()){
8 case R. id. buttonChangeDate:
9 MyDatePickerDialog myDatePickerDialog = new MyDatePickerDialog ();
10 DatePickerDialog datePickerDialog = new DatePickerDialog (MainActivity. this,
11 myDatePickerDialog, mDatePicker. getYear (), mDatePicker. getMonth (), mDatePicker. getDayOfMonth ());
12 datePickerDialog. show (); // display the date Setting Dialog Box
13 break;
14 case R. id. buttonChangeTime:
15 MyTimePickerDialog myTimePickerDialog = new MyTimePickerDialog ();
16 TimePickerDialog timePickerDialog = new TimePickerDialog (MainActivity. this,
17 myTimePickerDialog, mTimePicker. getCurrentHour (), mTimePicker. getCurrentMinute (), true );
18 timePickerDialog. show (); // display the Time Setting Dialog Box
19 break;
20}
21}
22
23 /*
24 * Function: customizes the mydatepickerdiener class, which is used to implement the DatePickerDialog. OnDateSetListener interface,
25 * this interface method is triggered when you click the "set" button in the date Setting dialog box.
26 * Author: blog Park-still indifferent
27 */
28 public class MyDatePickerDialog implements DatePickerDialog. OnDateSetListener {
29 public void onDateSet (DatePicker view, int year, int monthOfYear, int dayOfMonth ){
30 mDatePicker. updateDate (year, monthOfYear, dayOfMonth); // update the date value
31}
32}
33
34 /*
35 * Function: Customize the mytimepickerdiener class to implement the TimePickerDialog. OnTimeSetListener interface,
36 * this interface method is triggered when you click the "set" button in the Time Setting dialog box.
37 * Author: blog Park-still indifferent
38 */
39 public class MyTimePickerDialog implements TimePickerDialog. OnTimeSetListener {
40 public void onTimeSet (TimePicker view, int hourOfDay, int minute ){
41 mTimePicker. setCurrentHour (hourOfDay); // set the hour value of TimePicker
42 mTimePicker. setCurrentMinute (minute); // set the minute value of TimePicker
43}
44}