Chapter Content
Section 1th AnalogClock and DigitalClock
2nd Section CalendarView
Section 3rd DatePicker and Timerpicker
4th Section Chronometer
5th Section Timer class
This chapter aims
Master the use of graphics clocks and digital clocks.
Master the use of the calendar view.
Proficiency in the use of date and time selectors.
Master the usage of chronometer.
Master the usage of the Timer class.
AnalogClockis an analog clock view, using the AnalogClock label layout, in addition to the long and wide outside the basic needs of other properties, the corresponding Java class is android.widget.AnalogClock, layout examples are as follows:
<analogclock android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/>
DigitalclocK is a digital clock view, using the DigitalClock label layout, in addition to the length and width of the basic need for other properties, the corresponding Java class is android.widget.DigitalClock, layout examples are as follows:
<digitalclock android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/>
Calendar view CalendarView, which you can use to display and select dates
<calendarviewandroid:layout_width= "Match_parent" android:layout_height= "Match_parent" android:firstDayOfWeek= "3" android:shownweekcount= "4" android:selectedweekbackgroundcolor= "#aff" android:focusedmonthdatecolor= "#f00" Android:weekseparatorlinecolor= "#ff0" android:unfocusedmonthdatecolor= "#f9f" android:id= "@+id/calendarview"/>
Event, option Change event
public void Onselecteddaychange (CalendarView view, int year,int month, int dayofmonth) {// Use Toast to display user selected date Toast.maketext (Calendarviewtest.this, "Your birthday is" + year + "years" + month + "Month" + DayOfMonth + "Day", Toast.length_sh ORT). Show ();}});
DatePicker is a control for date selection and is laid out using DatePicker, with the following common properties:
Android:calendarviewshown indicates whether the full calendar is displayed
Android:endyear represents the maximum year that can be selected
Android:maxdate indicates the maximum date that the calendar displays
Android:spinnershown Indicates whether the Adjust arrow button is displayed
DatePicker is a control for date selection, and the corresponding class is Android.widget.DatePicker, which is commonly used in the following ways:
Init () listener to initialize display date and registration date selection changes
GetYear () to extract the selected year
GetMonth () to extract the selected month
GetDayOfMonth () to extract the selected day
The layout examples are as follows:
<datepicker android:id= "@+id/datepick" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:calendarviewshown= "true" />
Timepickeris a control for time selection, layout using Timepicker, except for the long and wide outside of the basic set of other properties
The corresponding class is android.widget.TimePicker, the common method is as follows:
Getcurrenthour () Gets the current hour
Getcurrentminute () Gets the current minute
Android.widget.TimePicker.OnTimeChangedListener for Event Listener interface
Timer chronometer, the component and DigitalClock inherit from TextView, so they all display a piece of text. But Chronometer does not show the current time, it shows how long it has been since a certain starting time.
Chronometer supports the following common methods.
SetBase (long Base): Sets the start time of the timer.
SetFormat (String format): Sets the format of the display time.
Start (): Start timing.
Stop (): Stop timing.
Setonchronometerticklistener (Chronometer.onchronometerticklistener Listener): Binds the event listener to the timer, triggering the listener when the timer changes.
Using the chronometer control to implement the operation of the meter, start
Set Start timing Time
Chronometer.setbase (Systemclock.elapsedrealtime ());
When you start to remember
Chronometer.start ();
Chronometer.stop (); Stop it
Timer timed Event triggered
Chronometer.setonchronometerticklistener (New Chronometer.onchronometerticklistener () {public void Onchronometertick (chronometer chronometer) { //If the start time is now more than startime seconds if (Systemclock.elapsedrealtime () -Chronometer.getbase () > StartTime * 1000) { chronometer.stop (); Prompt the user for ShowDialog (); } }
Making slides based on chronometer
Timer class
The timer class functions like the chronometer component and can execute program code at specific times and is more powerful than chronometer components
Grammar
The Timer object is executed in the Schedule method
Timer object name. Schedule (TimerTask object, delay time, interval time);
TimerTask Object : Is the Timer object executes the program code, the developer must write their own code to execute the program. Delay time: Set how long before the timer object starts executing, in milliseconds. Interval: Sets the interval for how long the TimerTask object is executed once, in milliseconds.
TimerTask class
The TimerTask object is the body of the timer object, which is the work that is used to define the timer object to perform at timed intervals. The program code in the Run method in the TimerTask object is a code block that executes repeatedly, and the syntax is
Private timertask variable name =new timertask () {public void run () { Execute program code ... }) ;
The handler object is a message mediator between different threads in the application, and the message object is sent out in the TimerTask object.
public void Run () { message message=new message (); message.what= send out the message; Handler.sendmessage (message); }
The syntax for the handler object to receive messages is:
Private Handler variable name =new Handler () {public void Handlemessage (Message msg { super.handlemessage (msg ); Switch (msg.what) {case receives message: program code break ; ) }} ;
Timer Stopwatch Initialization Timer
Timer timer=new timer ()//Create Timer Object Timer.schedule (new TimerTask () {//Create TimerTask object public void Run () {if (flag) {tsec++; Message msg=new messages ();//Create msg.what=1;//set type Handler.sendmessage (msg);//Send Message to Handler}}}, 0,1000);//start execution immediately, Time interval is 1000 milliseconds
Stopwatch, Accept Message
public void Handlemessage (message msg) {//Accept Message super.handlemessage (msg); switch (msg.what) {//Judgment message type case 1:csec=tsec%60 ;//Gets the number of seconds cmin=tsec/60;//gets the number of minutes String Str=string.format ("%02d:%02d", cmin,csec);//display data in 00:00 format Txtclock.settext (str) ; break;}}
Follow me to learn Android 9th time component