Android chronometer Control implements timer function detailed _android

Source: Internet
Author: User
Tags gettext set time

This article shows you how to use the Chronometer control to implement an instance of an Android timer.

Put the final implementation diagram first:

The idea of Android timer implementation

Use the chronometer control to implement the action of the meter. Set the initial time by setting SetBase (long base) and add a setonchronometerticklistener to it (Chronometer.onchronometerticklistener L) Event to determine whether the time is up, and then call its stop () method to stop the timer.

Android Timer Implementation code
Main.xml:

<?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:background=" @drawable/bac K "android:gravity=" center "android:orientation=" vertical "> <linearlayout android:layout_width=" fil L_parent "android:layout_height=" wrap_content "android:layout_margin=" 10dip "android:orientation=" Horizonta L "> <textview android:layout_width=" fill_parent "android:layout_height=" Wrap_content "a ndroid:layout_weight= "4" android:gravity= "Center" android:text= "Set Time:"/> <edittext Andr Oid:id= "@+id/edt_settime" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" and roid:layout_weight= "1" android:inputtype= "number"/> </LinearLayout> <chronometer android: Id= "@+id/chronometer" AndroId:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:gravity= "center" Android:textCol Or= "#ff0000" android:textsize= "60dip"/> <linearlayout android:layout_width= "Fill_parent" Androi d:layout_height= "Wrap_content" android:layout_margin= "10dip" android:orientation= "Horizontal" > <Bu Tton android:id= "@+id/btnstart" android:layout_width= fill_parent "android:layout_height=" Wrap_conten 
      T "android:layout_weight=" 1 "android:text=", "/>" <button android:id= "@+id/btnstop" 
      Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_weight= "1" 
      android:text= "Stop Time"/> <button android:id= "@+id/btnreset" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "reset"/> </linear Layout> </lineaRlayout>  

Activity code:

Package com.jiahui.chronometer;  
Import android.app.Activity;  
Import Android.app.AlertDialog;  
Import Android.app.Dialog;  
Import Android.content.DialogInterface;  
Import Android.os.Bundle;  
Import Android.os.SystemClock;  
Import Android.text.format.Time;  
Import Android.view.View;  
Import Android.widget.Button;  
Import Android.widget.Chronometer;  
 
Import Android.widget.EditText;  
 
  public class Chronometerdemoactivity extends activity {private int starttime = 0;  
    public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  
 
    Setcontentview (R.layout.main);  
 
    Final chronometer chronometer = (chronometer) Findviewbyid (r.id.chronometer);  
 
    Button btnstart = (button) Findviewbyid (R.id.btnstart);  
 
    Button Btnstop = (button) Findviewbyid (r.id.btnstop);  
 
    Button Btnrest = (button) Findviewbyid (R.id.btnreset);  
 
    Final EditText edtsettime = (edittext) Findviewbyid (r.id.edt_settime); Btnstart.setOnclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {SYSTEM.O  
        Ut.println ("--Start the time---");  
        String ss = Edtsettime.gettext (). toString (); if (!) ( Ss.equals ("") && SS!= null)) {starttime = Integer.parseint (Edtsettime.gettext (). ToS  
        Tring ());  
        //Set start time Chronometer.setbase (Systemclock.elapsedrealtime ());  
 
      Start Chronometer.start ();  
 
    }  
    }); Btnstop.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {/  
      /stop Chronometer.stop ();  
 
    }  
 
    });   
        Reset Btnrest.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {  
 
      Chronometer.setbase (Systemclock.elapsedrealtime ());  
    }  
 
    }); Chronometer. Setonchronometerticklistener (New CHRONOMETER.ONCHROnometerticklistener () {@Override public void Onchronometertick (chronometer chronometer) { If the start timer is now more than startime seconds if (Systemclock.elapsedrealtime ()-Chronometer.getbase ()  
              > StartTime * 1000) {chronometer.stop ();  
            Give the user prompt ShowDialog ();  
  }  
          }  
        });  
     } protected void ShowDialog () {Alertdialog.builder Builder = new Alertdialog.builder (this);  
    Builder.seticon (R.DRAWABLE.EB28D25);  
          Builder.settitle ("Warning"). Setmessage ("Time to"). Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {  
 
    @Override public void OnClick (Dialoginterface dialog, int which) {}});  
    Alertdialog dialog = Builder.create ();  
  Dialog.show ();  }  
}

The above is about the Android Chronometer control implementation Timer related functions, I hope to learn more about Android software programming help.

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.