When Android realizes phone verification, the button countdown 60s

Source: Internet
Author: User

Mobile phone registration will generally have a button, the default display to obtain a verification code, click and then become xx seconds after re-obtain the verification code

On the internet there are two ways to do this, one is to customize a Timebutton, the other is to use the encapsulated 60 seconds to obtain the Code tool class

First, add a line of code to the App.java class first

 Public Static Map<string, long> map; // time to hold the countdown

The Timebutton class is then added to the project, and the time default is 60s, which can then be modified in the code by the set method.

 Packagenet.zhomi.negotiation.view.utils;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Timer;ImportJava.util.TimerTask;Importnet.zhomi.negotiation.app.App;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classTimebuttonextendsButtonImplementsOnclicklistener {Private LongLenght = 60 * 1000;//countdown length, here gives the default of 60 seconds    PrivateString textafter = "re-fetch after seconds"; PrivateString Textbefore = "Click to get Verification code"; Private FinalString time = "Time"; Private FinalString CTIME = "CTIME"; PrivateOnclicklistener Monclicklistener; PrivateTimer t; PrivateTimerTask tt; Private LongTime ; PrivateContext Mcontext; Map<string, long> map =NewHashmap<string, long>();  PublicTimebutton (Context context) {Super(context); Setonclicklistener ( This); }     PublicTimebutton (Context context, AttributeSet attrs) {Super(context, attrs); Mcontext=context; Setonclicklistener ( This); } @SuppressLint ("Handlerleak") Handler Han=NewHandler () { Public voidhandlemessage (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 voidInittimer () { time=lenght; T=NewTimer (); TT=NewTimerTask () {@Override Public voidrun () {LOG.E ("Yung", time/1000 + ""); Han.sendemptymessage (0x01);    }        }; }    Private voidCleartimer () {Toast.maketext (Mcontext,"Timed out", Toast.length_short). Show (); if(TT! =NULL) {tt.cancel (); TT=NULL; }        if(t! =NULL) T.cancel (); T=NULL; } @Override Public voidSetonclicklistener (Onclicklistener l) {if(linstanceofTimebutton) {            Super. Setonclicklistener (l); } Else             This. Monclicklistener =l; } @Override Public voidOnClick (View v) {if(Monclicklistener! =NULL) Monclicklistener.onclick (v);        Inittimer ();  This. SetText (time/1000 +textafter);  This. setenabled (false); T.schedule (TT,0, 1000); //t.scheduleatfixedrate (task, delay, period);    }    /*** Synchronize with Activity's OnDestroy () method*/     Public voidOnDestroy () {if(App.map = =NULL) App.map=NewHashmap<string, long>();        App.map.put (time, time);        App.map.put (CTIME, System.currenttimemillis ());        Cleartimer (); LOG.E ("Yung", "OnDestroy"); }    /*** Synchronize with Activity's OnCreate () method*/     Public voidOnCreate (Bundle bundle) {LOG.E ("Yung", App.map + ""); if(App.map = =NULL)            return; if(App.map.size () <= 0)//This indicates that there are no last-completed timings            return; LongTime = 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 to be displayed when timed*/     PublicTimebutton settextafter (String text1) { This. Textafter =Text1; return  This; }    /*** Set the text before clicking*/     PublicTimebutton Settextbefore (String text0) { This. Textbefore =text0;  This. SetText (Textbefore); return  This; }    /*** Set to timing length * *@paramLenght * Time default milliseconds *@return     */     PublicTimebutton Setlenght (Longlenght) {         This. Lenght =lenght; return  This; }    /***/}

The use of Timebutton in a resource file is not much different from the button

<Net.zhomi.negotiation.view.utils.TimeButton//timebutton the package android:id= "@+id/btn_getcode"Android:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_marginright= "15DP"Android:layout_weight= "2"Android:background= "@null"Android:singleline= "true"Android:text= "Re-fetch after 60 seconds"Android:textcolor= "@color/balck"android:textsize= "@dimen/normal_text_size" />

The second Kind

First look at the packaged tool class

 Packagecom.example.timebtn;ImportAndroid.os.CountDownTimer;ImportAndroid.widget.Button; Public classTimercountextendsCountdowntimer {PrivateButton BNT;  PublicTimercount (LongMillisinfuture,LongCountdowninterval, Button BNT) {        Super(Millisinfuture, countdowninterval);  This. BNT =BNT; }     PublicTimercount (LongMillisinfuture,Longcountdowninterval) {        Super(Millisinfuture, countdowninterval); //TODO auto-generated Constructor stub} @Override Public voidonfinish () {//TODO auto-generated Method StubBnt.setclickable (true); Bnt.settext ("Get Verification Code"); } @Override Public voidOnTick (Longarg0) {        //TODO auto-generated Method StubBnt.setclickable (false); Bnt.settext (arg0/1000 + "second after re-acquisition"); }}

How to use:

Button btn =new Timercount (60000,btn); Timer.start ();

It seems that the second type of code is simpler, but it is recommended to use the first.

When Android realizes phone verification, the button countdown 60s

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.