Android interface basic components ------ timer, Android ------ Timer

Source: Internet
Author: User

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.

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.