Android SMS Verification Code control

Source: Internet
Author: User

Android SMS Verification Code control, easy to use in the project uniform style, unified prompt modification, personally think very useful

<span style= "FONT-SIZE:18PX;" >public class Smsbutton extends Button {/** whether to show */private Boolean mattached;/** wait time after sending verification code */private int delaytime = 60;/** button above the text */private string showtext = "get";/** button waits for the text to be displayed */private string delaytext = Formatetag + "Second"; * * Now is the countdown */private boolean isdelayed = false;private smsonclicklistener clicklistener;private int currentdelaytime = 0; private static final String Formatetag = "%d";p rivate Runnable mtimer = new Runnable () {@Overridepublic void run () {Curren Tdelaytime-= 1;if (currentdelaytime <= 0) {isdelayed = False;setclickable (True); SetText (showtext);} else {GetHandler (). postdelayed (Mtimer, +); Setdelaytimetext (Currentdelaytime);}}; private void Setdelaytimetext (int time) {SetText (String.Format (Delaytext, Time));} Public Smsbutton (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Initview ();} Public Smsbutton (context context, AttributeSet Attrs) {Super (context, attrs); Initview ();} Public SMsbutton (Context context) {super (context); Initview ();} @Overridepublic void Setonclicklistener (Onclicklistener l) {Super.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {if (!isdelayed) {//If there is no wait if (clicklistener! = null) {if (Clicklistener.validate ()) {setclickable (false); isdelayed = True;currentdelaytime = Delaytime;gethandler (). Post (Mtimer); Clicklistener.onclick ();}}}}});} /** * Initialize page */private void Initview () {String text = GetText (). ToString (). ToString (). Trim (); if (text! = NULL &&!) ". Equals (text)) {showtext = text;}} @Overrideprotected void Onattachedtowindow () {Super.onattachedtowindow (); if (!mattached) {mattached = True;settext ( Showtext);}} @Overrideprotected void Ondetachedfromwindow () {Super.ondetachedfromwindow (); if (mattached) {mattached = false; isdelayed = False;gethandler (). Removecallbacks (Mtimer);}} public int GetDelayTime () {return delaytime;} public void SetDelayTime (int delaytime) {this.delaytime = Delaytime;} Public String GetshowtexT () {return showtext;} public void Setshowtext (String showtext) {this.showtext = Showtext;} Public String Getdelaytext () {return delaytext;} public void Setdelaytext (string start, String end) {This.delaytext = start + Formatetag + end;} Public Smsonclicklistener Getsmsonclicklistener () {return clicklistener;} public void Setsmsonclicklistener (Smsonclicklistener clicklistener) {setonclicklistener (null); This.clicklistener = Clicklistener;} Public interface Smsonclicklistener {public boolean validate ();p ublic void OnClick (); </span>
Usage:

/** * Example * *  Smsbutton = (Smsbutton) Findviewbyid (R.id.smsbutton); * Smsbutton.setshowtext ("get"); * Smsbutton.setdela Ytext ("et", "SEC");  Smsbutton.setsmsonclicklistener (NEW * Smsonclicklistener () {*  * * @Override public Boolean validate () {return true;} * @Override public void OnClick () {*           toastutil.getinstance (). Toastincenter (loadingactivity.this, *           "onclick" ); } }); */



Android SMS Verification Code control

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.