Android --- 25 --- Chronometer timer, android Timer

Source: Internet
Author: User
Tags getbase

Android --- 25 --- Chronometer timer, android Timer
Chronometer Timer
It provides an android: format attribute to specify the timer format.
SetBase (long base): Set the start time
SetFormat (String format): Set the display time format
Start (): start timing
Stop (): stop timing

SetOnchronometerTickListener (): binds the time listener to the timer. This listener is triggered when the timer changes.


The time is 20 s. When the time is reached, the time is stopped.

MainActivity. java:


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 {private Chronometer ch; private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ch = (Chronometer) findViewById (R. id. test); button = (Button) findViewById (R. id. button); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // set the start time ch. setBase (SystemClock. elapsedRealtime (); // start timing ch. start (); // The button is unavailable. setEnabled (false) ;}}); ch. listener (new OnChronometerTickListener () {@ Overridepublic void onChronometerTick (Chronometer chronometer) {// TODO Auto-generated method stub // if the time exceeds 20sif (SystemClock. elapsedRealtime ()-ch. getBase ()> 20*1000) {// stop timing ch. stop (); // The button is available. setEnabled (true );}}});}}


Activity_main.xml:


<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = "com. example. chronometerdemo2.MainActivity "> <Chronometer android: id =" @ + id/test "android: layout_width =" wrap_content "android: layout_height =" wrap_content "/> <Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "start"/> </LinearLayout>


Custom time:


MainActivity. java:


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 implements OnClickListener {private Chronometer ch;private Button start, stop;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ch = (Chronometer) findViewById(R.id.test);start = (Button) findViewById(R.id.start);stop = (Button) findViewById(R.id.stop);start.setOnClickListener(this);stop.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.start:ch.setBase(SystemClock.elapsedRealtime());ch.start();start.setEnabled(false);break;case R.id.stop:ch.stop();start.setEnabled(true);break;default:break;}}}


Activity_main.xml:


<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = "com. example. chronometerdemo. mainActivity "> <Chronometer android: id =" @ + id/test "android: layout_width =" wrap_content "android: layout_height =" wrap_content "/> <LinearLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: id = "@ + id/start" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> <Button android: id = "@ + id/stop" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "stop"/> </LinearLayout>


The following is a timer I made myself, but I have a question. I hope you can answer it ..


MainActivity. java:


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. textView; public class MainActivity extends Activity implements OnClickListener {private Button start, stop, reset; private Chronometer ch; private TextView show; private String tip = ""; private int num = 0; private Boolean flag = false; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ch = (Chronometer) findViewById (R. id. ch); start = (Button) findViewById (R. id. start); stop = (Button) findViewById (R. id. stop); reset = (Button) findViewById (R. id. reset); start. setOnClickListener (this); stop. setOnClickListener (this); reset. setOnClickListener (this); show = (TextView) findViewById (R. id. show) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. start: ch. setBase (SystemClock. elapsedRealtime (); ch. start (); start. setEnabled (false); flag = true; break; case R. id. stop: if (flag = true) {num ++; long time = SystemClock. elapsedRealtime ()-ch. getBase (); tip ++ = num + "count" + time/1000 + ". "+ time % 1000 +" s \ n "; show. setText (tip);} break; case R. id. reset: ch. stop (); flag = false; show. setText (""); num = 0; tip = ""; start. setEnabled (true); break; default: break ;}}}

Activity_main.xml:


<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = "com. example. chronometerdemo3.MainActivity "> <Chronometer android: id =" @ + id/ch "android: layout_width =" wrap_content "android: layout_height =" wrap_content "/> <TextView android: id = "@ + id/show" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: hint = "Timing duration"/> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: id = "@ + id/start" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> <Button android: id = "@ + id/stop" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "count"/> <Button android: id = "@ + id/reset" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "reset"/> </LinearLayout>




The Code is the above. The problem is that I want to put the TextView in the layout file under the nested LinearLayout. According to my understanding, there should be no problem, and the compiler does not report an error, but when running on the simulator, It is a crash... I don't know where the problem is...


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.