Android uses Countdowntimer to achieve countdown effect _android

Source: Internet
Author: User

In the development will often use the countdown function, including the phone to send verification code, and so on, before my practice is to use handler + Timer + TimerTask to achieve, now found this class, decisively abandoned before the practice, I'm sure there are a lot of people like me. At first I didn't know that Android had helped us encapsulate a class called Countdowntimer.

Literally, it's called a countdown timer, called a timer or timer, implemented in a handler way, encapsulating the creation of background threads and handler queues.
Looked at the source code, found that this class call is quite simple, there are only four methods:

(1) public abstract void Ontick (long millisuntilfinished);
Fixed interval is invoked

(2) public abstract void onfinish ();
Called when the countdown is complete

(3) Public synchronized final void cancel ():
Cancel the countdown, and when it starts again, the countdown will start again.

(4) Public synchronized final Countdowntimer start ():
Start Countdown

Here you can see that the first two are abstract methods that need to be overridden.

Take a quick look at the code:

Package Com.per.countdowntimer;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.CountDownTimer;
Import Android.view.View;


Import Android.widget.TextView;

  public class Mainactivity extends activity {private TextView mtvshow;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
  Mtvshow = (TextView) Findviewbyid (r.id.show);
  /** * Cancel Countdown * @param v */public void OnCancel (View v) {timer.cancel ();
  /** * Start Countdown * @param v */public void restart (View v) {timer.start (); Private Countdowntimer timer = new Countdowntimer (10000, 1000) {@Override public void Ontick (Long millisunt
    ilfinished) {Mtvshow.settext (millisuntilfinished/1000) + "After seconds can be sent back");
      @Override public void OnFinish () {mtvshow.setenabled (true);
    Mtvshow.settext ("Obtain Verification Code");
}
  };

 }

Incidentally, attach an XML layout file

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
  android:layout_width= "Match_ Parent "
  android:layout_height=" match_parent "
  android:background=" @android: Color/white "
  android:o rientation= "vertical"
  android:padding= "16DP" >

  <textview
    android:id= "@+id/show"
    android: Layout_width= "Wrap_content"
    android:layout_height= "wrap_content"
    android:text= "@string/hello_world" >

  <button
    android:layout_width= "wrap_content"
    android:layout_height= "Wrap_content"
    android:layout_margintop= "10DP"
    android:onclick= "restart"
    android:text= "Cancel"/>

  <button
    android:layout_width= "wrap_content"
    android:layout_height= "wrap_content"
    android:layout_ margintop= "10DP"
    android:onclick= "OnCancel"
    android:text= "End"/>

</LinearLayout>

Finally, explain:
Countdowntimer timer = new Countdowntimer (10000, 1000): In milliseconds, the first parameter refers to the number of milliseconds from the start of the call to the start () method when the OnFinish () method is called at the time the countdown completes , which is the total time of the countdown; the second parameter indicates how many milliseconds to call the Ontick method at intervals, such as 1000 milliseconds apart.
Use Timer.start () directly at the time of the call;

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.