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