The Chronometer component implements the timer Applet and the chronometer timer.
The main. xml Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <Chronometer android: id = "@ + id/myChronometer" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <LinearLayout android: orientation = "horizontal" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <Button android: id = "@ + id/btnStart" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "start timing"/> <Button android: id = "@ + id/btnStop" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Stop timing"/> <Button android: id = "@ + id/btnBase" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "reset"/> <Button android: id = "@ + id/btnFormat" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> </LinearLayout>
The. java code is as follows:
Package org. lxh. demo; import android. app. activity; import android. OS. bundle; import android. OS. systemClock; import android. view. view; import android. view. view. onClickListener; import android. view. view. onFocusChangeListener; import android. widget. button; import android. widget. chronometer; import android. widget. editText; import android. widget. textView; public class Hello extends Activity {private Chronometer my Chronometer = null; private Button btnStart = null; private Button btnStop = null; private Button btnBase = null; private Button btnFormat = null; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // life cycle method super. setContentView (R. layout. main); // set the layout manager to use this. myChronometer = (Chronometer) super. findViewById (R. id. myChronometer); this. btnStart = (Button) super. findViewById (R. id. btnSta Rt); this. btnStop = (Button) super. findViewById (R. id. btnStop); this. btnBase = (Button) super. findViewById (R. id. btnBase); this. btnFormat = (Button) super. findViewById (R. id. btnFormat); this. btnStart. setOnClickListener (new OnClickListenerStart (); this. btnStop. setOnClickListener (new OnClickListenerStop (); this. btnBase. setOnClickListener (new OnClickListenerBase (); this. btnFormat. setOnClickListener (new OnClickListen ErFormat ();} private class OnClickListenerStart implements OnClickListener {public void onClick (View arg0) {Hello. this. myChronometer. start () ;}} private class OnClickListenerStop implements OnClickListener {public void onClick (View arg0) {Hello. this. myChronometer. stop () ;}} private class OnClickListenerBase implements OnClickListener {public void onClick (View arg0) {Hello. this. myChronometer. setBase (Syste MClock. elapsedRealtime () ;}} private class OnClickListenerFormat implements OnClickListener {public void onClick (View arg0) {Hello. this. myChronometer. setFormat ("New Display format: % s. ");}}}
The running effect is as follows: