A simple package for Android to get the verification code countdown 60 seconds button

Source: Internet
Author: User

Since it is often troublesome to need a countdown button when registering for a verification code, take some time to write a Timebutton

The key mouse characteristics,

Timebutton use the same time as the normal button without conflict,

Timebutton back in the countdown, and if there's not more time left to go, it'll keep running.

Timebutton inside the button click Display and Countdown logic,

Timebutton will be destroyed after the activity is destroyed, and will not run in the background like a thread.

Timebutton display text and countdown can be set on their own, of course, there are default,
Original Address http://write.blog.csdn.net/postedit/43563033

To post the code, you first need to define a map in application

public class App extends application {//For holding countdown time public static map<string, long> Map;}
And then use it in the activity
Package Com.yung.timebutton;import Android.app.activity;import Android.os.bundle;import android.view.View;import         Android.view.view.onclicklistener;import android.widget.toast;/** * Test Main interface * * @author yung * <P> * January 14, 2015 13:00:26 */public class Mainactivity extends Activity implements Onclicklistener {private Timebutton V; @Overri deprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); v = (Timebutton) Findviewbyid (R.id.button1); v.oncreate (savedinstancestate); v.setTextAfter ("Regain after Seconds"). Settextbefore ("Click to get Verification Code"). Setlenght (*); V.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, " This is the process caller Onclicklistnenr ", Toast.length_short). Show (); @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubv.ondestroy (); Super.ondestroy ();}}

The next step is Timebutton's code.

Package Com.yung.timebutton;import Java.util.hashmap;import Java.util.map;import java.util.timer;import Java.util.timertask;import Android.annotation.suppresslint;import Android.content.context;import Android.os.bundle;import Android.os.handler;import Android.util.attributeset;import Android.util.Log;import Android.view.view;import Android.view.view.onclicklistener;import android.widget.button;/** * Given the frequent use of the Get Verification Code countdown button         On the internet also did not find the ideal of their own write a * * * @author yung * <P> * January 14, 2015 [Buddha bless never bug] * <p> * PS: Due to the discovery of the timer after each cancle () can not re-schedule method, so time is complete only fear timer. * Reset the timer every time you start the timer. * Note that the class of OnCreate () OnDestroy () and Activity OnCreate () OnDestroy () are synchronized * */pub Lic class Timebutton extends Button implements Onclicklistener {private long lenght = 60 * 1000;//countdown length, here is the default of 60 seconds private String textafter = "Regain ~ after Seconds";p rivate string textbefore = "Click Get verification Code ~";p rivate final String time = "Time";p rivate final Strin G CTIME = "CTIME";p rivate onclicklistEner monclicklistener;private Timer t;private timertask tt;private long time; map<string, long> map = new hashmap<string, long> ();p ublic Timebutton (context context) {super (context); Setonclicklistener (this);} Public Timebutton (context context, AttributeSet Attrs) {Super (context, attrs); Setonclicklistener (this);} @SuppressLint ("Handlerleak") Handler Han = new Handler () {public void Handlemessage (Android.os.Message msg) { TimeButton.this.setText (time/1000 + textafter); time-= 1000;if (Time < 0) {TimeButton.this.setEnabled (true); TimeButton.this.setText (Textbefore); Cleartimer ();};}; private void Inittimer () {time = Lenght;t = new Timer (); TT = new TimerTask () {@Overridepublic void run () {LOG.E ("Yung", TI me/1000 + ""); Han.sendemptymessage (0x01);}};} private void Cleartimer () {if (TT! = null) {tt.cancel (); TT = NULL;} if (t = null) t.cancel (); t = null;} @Overridepublic void Setonclicklistener (Onclicklistener l) {if (L instanceof Timebutton) {Super.setonclicklistener (L);} Elsethis.monClicklistener = l;} @Overridepublic void OnClick (View v) {if (Monclicklistener! = null) Monclicklistener.onclick (v); Inittimer (); This.settext (time/1000 + textafter); this.setenabled (false); T.schedule (TT, 0, +);//T.scheduleatfixedrate (task, delay, period);} /** * and Activity's OnDestroy () method synchronize */public void OnDestroy () {if (App.map = = null) App.map = new hashmap<string, long> () ; App.map.put (time, time); App.map.put (CTIME, System.currenttimemillis ()); Cleartimer (); LOG.E ("Yung", "OnDestroy");} /** * and Activity's OnCreate () method synchronize */public void OnCreate (bundle bundle) {LOG.E ("Yung", App.map + ""), if (App.map = = null) retu Rn;if (App.map.size () <= 0)//This indicates that there is no last-completed timing return;long time = System.currenttimemillis ()-App.map.get (CTIME)- App.map.get (time); App.map.clear (); if (Time > 0) return;else {inittimer (); this.time = Math.Abs (time); T.schedule (TT, 0, 1000); This.settext (time + textafter); this.setenabled (false);}} /** * Set the text displayed when timed */public Timebutton settextafter (String text1) {this.textafter = Text1; return this;} /** * Set the text before clicking */public timebutton settextbefore (String text0) {This.textbefore = Text0;this.settext (Textbefore); return this;} /** * Set to timing length * * @param lenght * Time default milliseconds * @return */public Timebutton setlenght (long lenght) {this.lenght = Lenght;return this;} /***/}

The code is a little bit rotten ha, finish the project to upload
Project Address

http://download.csdn.net/detail/yung7086/8428517

Original address
http://write.blog.csdn.net/postedit/43563033

A simple package for Android to get the verification code countdown 60 seconds button

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.