Learn to use the Android chronometer timer _android

Source: Internet
Author: User
Tags string format

This example for you to share the Android Chronometer timer Basic use method, for your reference, the specific content as follows

By default, the Chronometer component outputs only the MM:SS or H:MM:SS time format. For example, when the timer is 1 minutes 20 seconds, the Chronometer component displays 01:20. If you want to change the content of the displayed information, you can use the SetFormat method of the Chronometer class. The method requires a string variable and uses '%s ' to represent timing information. For example, using SetFormat (Timing information:%s) to set display information, the Chronometer component displays the following timing information:

Timing Information: 10:20

android:format;//defines the format of the time such as: Hh:mm:ss
 setformat ("Timing:%s");//Set the display format
 setformat (String format);//Set the format of the display time. Start timer ()// 
Stop Timer setbase ()//

set base time, usually Systemclock.elapsedrealtime () 
Setonchronometerticklistener (Chronometer.onchronometerticklistener Listener)///When the timer changes, call

Case:

1. Define layout file Chronometer.xml

 <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android="
  Schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " android:orientation= "vertical" > <chronometer android:id= "@+id/chronometer" android:layout_width= "Match_pa" Rent "android:layout_height=" wrap_content "/> <button android:id=" @+id/chronometer_start "android:l Ayout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Start timing"/> <Button Id:id= "@+id/chronometer_end" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:
    text= "Stop timing"/> <button android:id= "@+id/chronometer_null" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "Restart timings"/> </linearlayout> 

2.java code file: Chronometerdemo.java

Package com.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.SystemClock;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
 
Import Android.widget.Chronometer;
   
  public class Chronometerdemo extends activity {private chronometer chronometer;
   
  Private Button Button_start, Button_end,button_bull; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.oncreate
     
    (savedinstancestate);
     
    Setcontentview (R.layout.chronometer);
    Chronometer = (chronometer) Findviewbyid (r.id.chronometer);
    Button_start = (Button) Findviewbyid (R.id.chronometer_start);
    Button_end = (Button) Findviewbyid (r.id.chronometer_end);
     
    Button_bull = (Button) Findviewbyid (r.id.chronometer_null);
    Button_start.setonclicklistener (Clicklistener);
    Button_end.setonclicklistener (Clicklistener);
 Button_bull.setonclicklistener (Clicklistener); Private Onclicklistener Clicklistener = new Onclicklistener () {@Override public void OnClick (View v) {switch (V.getid ()) {case R.id.chronometer_start://Invoke the Start () method to start the timer Chronometer.sta
        RT ();
        Button_start.settext ("timing ...");
      Break
        Case R.id.chronometer_end://Invoke Stop () method to stop timing chronometer.stop ();
        Button_start.settext ("Keep timing");
      Break
        Case R.id.chronometer_null://Invoke Stop () method to stop timing chronometer.setbase (Systemclock.elapsedrealtime ());
        Chronometer.start ();
        Button_start.settext ("timing ...");
 
      Break
      Default:break;
}
    }
  }; }

3. Operation Effect:

One problem is that after the timer starts, after a period of time, click to stop the timer, after a period of time, click to continue the timing, but this time is not the time after the stop. The timer still runs in the background after the instructions have stopped.
This is the entire content of this article, I hope to learn more about the Android Chronometer timer help and inspiration.

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.