Usage of the timer (Chronometer) and the timer chronometer

Source: Internet
Author: User
Tags getbase

Usage of the timer (Chronometer) and the timer chronometer

Android provides a timer component: Chronometer, Which is extends TextView. Therefore, a piece of text is displayed, but the display time is from the start time, it only provides the android: format attribute for specifying the counting format of the timer.

Chronometer is easy to use. It supports the following usage:

GetBase (): return time.

SetBase (long base): Set the start time of the timer.

Start (): start timing.

Stop (): stop timing.

SetFormat (String format): Set the display time format.

SetOnChronometerTickListener (Chronometer. OnChronometerTickListener listener): binds a listener event to the timer.

 

The following program designs a one-minute timer.

First:Define a Chronomete and Button component in the XML file:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "match_parent" android: layout_height = "match_parent"> <Chronometer android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/chronometer" android: textColor = "# ff0303" android: textSize = "12pt" android: layout_gravity = "center_horizontal" android: focusable = "true"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "start" android: id = "@ + id/button" android: layout_gravity = "center_horizontal"/> </LinearLayout>

Second:Use the setOnChronometerTickListener (Chronometer. OnChronometerTickListener listener) function to bind a listener event to the timer. The program defines a Chronometer component and a Button component. When you click a Button, the timer starts. The timer time is one minute.

Package happy. chronometer; import android. OS. bundle; import android. OS. systemClock; import android. support. design. widget. floatingActionButton; import android. support. design. widget. snackbar; import android. support. v7.app. appCompatActivity; import android. support. v7.widget. toolbar; import android. view. view; import android. view. menu; import android. view. menuItem; import android. widget. button; import android. widget. chronometer; public class MainActivity extends AppCompatActivity {Chronometer ch; Button start; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. test); // obtain the timer component ch = (Chronometer) findViewById (R. id. chronometer); // obtain the start component start = (Button) findViewById (R. id. button); start. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// set the start time ch. setBase (SystemClock. elapsedRealtime (); // start the timer ch. start () ;}}); // bind the listener event ch to the timer. setOnChronometerTickListener (new Chronometer. onChronometerTickListener () {@ Override public void onChronometerTick (Chronometer ch) {// if it has exceeded 60 s since the start of the timer if (SystemClock. elapsedRealtime ()-ch. getBase ()> 60*1000) {ch. stop (); start. setEnabled (true );}}});}}

 Finally:We can see the above effect, in which time is the time that has elapsed.

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.