Android Implementation Countdown

Source: Internet
Author: User

1, timer implementation code

public class mainactivity extends activity implements onclicklistener{   private EditText inputet; private Button getTime,startTime,stopTime;  private textview time; private int i = 0; private timer  timer = null; private timertask task = null;     @ Override    protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview (R.layout.activity_main);         Initview ();    }        private void  Initview () {     inputet =  (EditText)  findviewbyid (R.id.inputtime);         gettime =  (Button)  findviewbyid (r.id.gettime);         startTime =  (Button)  findviewbyid (r.id.starttime);         stopTime =  (Button)  findviewbyid (r.id.stoptime);         time =  (TextView)  findviewbyid (r.id.time);         gettime.setonclicklistener (This);         Starttime.setonclicklistener (this);         stoptime.setonclicklistener (this);    }  @Override  public void onclick (view v)  {   switch  (V.getid ())  {  case r.id.gettime:   time.settext ( Inputet.gettext (). ToString ());    i = integer.parseint (Inputet.gettext (). ToString ());    break;  case&nBsp R.id.starttime:   starttime ();    break;  case r.id.stoptime:    stoptime ();    break;  } }  private handler  mhandler = new handler () {  public void handlemessage (Message msg )  {   time.settext (msg.arg1+ "");    starttime ();  }; };   public void starttime () {  timer = new timer ();   task =  new timertask ()  {       @Override    public  Void run ()  {    i--;    message  message =  mhandler.obtainmessage ();    message.arg1 = i;     Mhandler.sendmessage (message);    }  };  timer.schedule (task, 1000);  }  public&nbSp;void stoptime () {  timer.cancel ();  }    } 

Android Implementation Countdown

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.