Android interface basic components ------ timer, Android ------ Timer
The timer inherited from the TextView component is used to display the total time of the text from a certain time. Because this component inherits from TextView, content is displayed in text format.
This component is also very simple to use. Generally, it inherits the following five methods:
1. setBase () sets the start time of the timer.
2. setFotmat () sets the display time format
3. start () specifies the start time
4. stop () specifies to suspend
5. setOnChronometerTickListener () binds the timer to the event listener, which is triggered when the timer changes
The following is a simple timer used to display the "used time ":
1> Add a timer to the new layout.
<Chronometer android:id="@+id/chronometer1" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chronometer" />
2> get the timer in the main activity, set the start time, display format, and start the timer.
Ch = (Chronometer) findViewById (R. id. chronometer1); ch. setBase (SystemClock. elapsedRealtime (); ch. setFormat ("used time: % s"); ch. start ();
3> bind a listener to the timer. Note that the event at this time is not caused by user operations. The timer event listening is the response after the timer is changed.
ch.setOnChronometerTickListener(new OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer arg0) { if(SystemClock.elapsedRealtime() - ch.getBase() >= 10000){ ch.stop(); } } });
Conclusion: The timer is mainly used to execute the timer itself. It should be noted that the event listener no longer listens to the user's action as the previous component, the listener is executed when the timer changes.