Android with Countdown control chronometer use method detailed _android

Source: Internet
Author: User
Tags getbase set time string format

Company's previous project, see Using this Android with the countdown control chronometer, now integrates

First look at the effect:

<chronometer 
  android:id= "@+id/chronometer" 
  android:layout_width= "wrap_content" 
  android:layout_ height= "30DP"/> 
 <button 
  android:onclick= "Start" 
  android:text= "starting" 
  android:layout_width= " Wrap_content " 
  android:layout_height=" wrap_content "/> 
 <button android:onclick= 
  " Stop " 
  android:text= "Stop" 
  android:layout_width= "wrap_content" 
  android:layout_height= "Wrap_content"/> 
 <button 
  android:onclick= "reset" 
  android:text= "reset" 
  android:layout_width= "Wrap_content" 
  android:layout_height= "Wrap_content"/> 

Method Introduction:

 long getbase ();//return base time, set by SetBase (long), can be a systemclock.elapsedrealtime () String GetFormat ()//returns the current string format, which is a void setbase (long base) implemented via SetFormat ();//Set time, value of the count timer specified void SetFormat (string format) //Set What is displayed, the timer will show the value of this argument, and if the string is null, the value returned is the MM:SS format 
Private chronometer chronometer; 
 The total time recorded by private long recordtime;// 
 @Override 
 protected void onCreate (Bundle savedinstancestate) { 
  Super.oncreate (savedinstancestate); 
  Setcontentview (r.layout.activity_main); 
  Chronometer= (chronometer) Findviewbyid (r.id.chronometer); 
  Chronometer.setformat ("Timing:%s");//Setting the format displayed 
  toast.maketext (mainactivity.this, "" +recordtime, Toast.length_short ). Show (); 
 public void Start (view view) { 
  chronometer.setbase (systemclock.elapsedrealtime ()-recordtime);//Skip the time that has been recorded 
  Chronometer.start (); 
 } 
 public void Stop (view view) { 
  chronometer.stop (); 
  Recordtime=systemclock.elapsedrealtime ()-chronometer.getbase ()//time to save this record 
 } public 
 void Reset (View View) { 
  recordtime=0;//reset time 
  chronometer.setbase (Systemclock.elapsedrealtime ()); 
 } 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.