Implementation method of Chronometer timer for Android control _android

Source: Internet
Author: User

Chronometer is a simple timer, you can give it a start time and take this timed, or if you don't give it a start time, it will start with your time call. By default it appears in the form of the current timer's value in "Minutes: Seconds" or "h:mm:ss", or in a set (string) format that can be used with a timer value to an arbitrary string

1. Important Attributes

Android:format: Defines the format of the time as: Hh:mm:ss

2. Important Methods

SetBase (long Base): Set countdown timer

SetFormat (String format): Sets the format for displaying the time.

Start (): Begin timing

Stop () : Stopping timing

Setonchronometerticklistener (Chronometer.onchronometerticklistener Listener): Called when the timer changes.

3. Examples

Layout file

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:padding=" 4dip "android:gravity=" Center_horizontal "Android:layout_
 Width= "Match_parent" android:layout_height= "match_parent" > <chronometer android:id= "@+id/chronometer" android:format= "Initial format:" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:
 layout_weight= "0" android:paddingbottom= "30dip" android:paddingtop= "30dip"/> <button android:id= "@+id/start" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Start" > <requestFocus/&
 Gt </Button> <button android:id= "@+id/stop" android:layout_width= wrap_content "android:layout_height=" Wrap_ Content "android:text=" Stop "> </Button> <button android:id=" @+id/reset "android:layout_width=" Wrap_conten T "android:layout_height=" wrap_content "android:text="Reset" > </Button> <button android:id= "@+id/set_format" android:layout_width= "Wrap_content" android:layout _height= "wrap_content" android:text= "Formatting" > </Button> <button android:id= "@+id/clear_format" Android:la Yout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "purge format" > </Button> </ 
 Linearlayout>

Main program:

Package WJQ.

Widgetdemo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.SystemClock;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

Import Android.widget.Chronometer;
 public class Chronometerdemo extends activity {private chronometer mchronometer; /* (non-javadoc) * @see android.app.activity#oncreate (android.os.Bundle)/@Override protected void onCreate (Bundle s
 Avedinstancestate) {//TODO auto-generated Method Stub super.oncreate (savedinstancestate);
 Setcontentview (R.layout.chronometerpage);

 Button button;

 Mchronometer = (chronometer) Findviewbyid (r.id.chronometer);
 Watch for button clicks.
 Button = (button) Findviewbyid (R.id.start);

 Button.setonclicklistener (Mstartlistener);
 Button = (button) Findviewbyid (r.id.stop);

 Button.setonclicklistener (Mstoplistener);
 Button = (button) Findviewbyid (R.id.reset);

 Button.setonclicklistener (Mresetlistener); Button = (button) Findviewbyid (r.iD.set_format);

 Button.setonclicklistener (Msetformatlistener);
 Button = (button) Findviewbyid (R.id.clear_format);
 Button.setonclicklistener (Mclearformatlistener);
 } view.onclicklistener Mstartlistener = new Onclicklistener () {public void OnClick (View v) {mchronometer.start ();

 }
 };
 View.onclicklistener Mstoplistener = new Onclicklistener () {public void OnClick (View v) {mchronometer.stop ();

 }
 }; View.onclicklistener Mresetlistener = new Onclicklistener () {public void OnClick (View v) {mchronometer.setbase (SystemC
 Lock.elapsedrealtime ());

 }
 }; View.onclicklistener Msetformatlistener = new Onclicklistener () {public void OnClick (View v) {Mchronometer.setformat ("
 Formatted time (%s));

 }
 }; View.onclicklistener Mclearformatlistener = new Onclicklistener () {public void OnClick (View v) {Mchronometer.setformat
 (null);
}
 };
 }

  above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud-dwelling community.

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.