Android study note 23: use of the time and date Control

Source: Internet
Author: User
Tags xml attribute

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); // 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; 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 = New  Mydatepickerdialog ();  10 Datepickerdialog = New Datepickerdialog (mainactivity. This  ,  11   Mydatepickerdialog, mdatepicker. getyear (), mdatepicker. getmonth (), mdatepicker. getdayofmonth ());  12 Datepickerdialog. Show (); //  Show Date Settings dialog box 13               Break  ;  14           Case  R. Id. buttonchangetime:  15 Mytimepickerdialog = New  Mytimepickerdialog ();  16 Timepickerdialog = New Timepickerdialog (mainactivity. This  ,  17 Mytimepickerdialog, mtimepicker. getcurrenthour (), mtimepicker. getcurrentminute (), True  );  18 Timepickerdialog. Show (); //  Show time Settings dialog box  19               Break  ;  20   }  21   }  22       23       /* 24   * Function: customizes the mydatepickerdiener class to implement the datepickerdialog. ondatesetlistener interface,  25   * This interface method is triggered when you click "set" 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 date value  31   }  32   }  33       34       /*  35   * Function: customizes 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 hourly value of timepicker  42 Mtimepicker. setcurrentminute (minute ); //  Set the minute value of timepicker 43   }  44 }

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.