TextView of UI components and their subclasses (v) Timers chronometer

Source: Internet
Author: User

Chronometer directly inherits the TextView component, which displays a piece of text that shows how long it has been since some time. We look at the source of chronometer:


It can be seen that the timer is directly inherited TextView, of course, can use all the properties and methods of TextView. At the same time it also has its own internal interface Onchronometerticklistener

New properties and methods for timers:


The usual methods for timers are as follows:

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 (Onchronometerticklistener listener); Binds a time listener to the timer, triggering the listener when the timer changes

The following demo, click on the button to start the time, click again is to stop the timing, show this period. The part of the comment is when the user clicks the button and the system starts to time, when the timer stops for more than 20 seconds.

<?xml    Version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:id= "@+id/root" android:layout_width= "match_parent" android:layout_height= "Match_parent" android:gravity= "        Center_horizontal "android:orientation=" vertical "> <chronometer android:id=" @+id/chronometer1 "    Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Chronometer"/> <button android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "wrap _content "android:text=" button "/></LINEARLAYOUT> 
Package Com.example.chronometertest;import Android.app.activity;import Android.os.bundle;import Android.os.systemclock;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.chronometer;import    Android.widget.chronometer.onchronometerticklistener;public class Mainactivity extends Activity {chronometer ch;    Button btn;    int flag=0;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Get the timing Component Ch= (chronometer) Findviewbyid (r.id.chronometer1);                Btn= (Button) Findviewbyid (R.id.button1); Btn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stubif (flag==0) {//Set start time, click button to start, Stop ch.setbase at point (Systemclock.elapsedrealtime ()); Ch.start ();//start flag=1;} Else{ch.stop ();//stop flag=0;} /*Set start time Ch.setbase (Systemclock.elapsedrealtime ()); Ch.start (); btn.setenabled (false);//button settings not available */}}); Set the event listener for chronometer, when the timer change is triggered by the listener/* Ch.setonchronometerticklistener (New Onchronometerticklistener () {@Override public void Onchronometertick (chronometer chronometer) {//TODO auto-generated method stub//If the time from now on is longer than 20 seconds if (                Systemclock.elapsedrealtime ()-chronometer.getbase () >20*1000) {ch.stop (); btn.setenabled (True);}} }); */} @Override public boolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; thi        s adds items to the action bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml. Int id = item.getitemid ();        if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}






TextView of UI components and their subclasses (v) Timers chronometer

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.