The Chronometer component implements the timer Applet and the chronometer timer.

Source: Internet
Author: User

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:


Related Article

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.