Android send a verification code countdown button

Source: Internet
Author: User
<span id="Label3"></p><p><p>1 Direct</p></p><p><p><br></p></p><p><p>2 principle</p></p><p><p>The simple principle is to encapsulate the corresponding countdown logic into a control and provide an interface to the Outside.</p></p><p><p>3 Code</p></p><p><p></p></p><pre name="code" class="java">Import Java.util.timer;import java.util.timertask;import Android.content.context;import Android.os.Handler;import Android.os.message;import Android.util.attributeset;import Android.view.layoutinflater;import android.view.View; Import android.widget.framelayout;/** * Custom controls to achieve the countdown Function. * * @author Admin * */public class Daojishi extends framelayout {private static final int jishi = 0x000;private smartbut Ton9 daojishi;private Handler handler;private Timer timer;private int alltime = 50;private Daojishicallback daojishicallb Ack;public Daojishi (context context, attributeset attrs, int defstyle) {super (context, attrs, Defstyle);//TODO auto-gene Rated constructor Stublayoutinflater inflater = (layoutinflater) Context.getsystemservice (context.layout_inflater_ SERVICE); inflater.inflate (r.layout.daojishi, this); initview (); inithandler (); initlisterner ();} Public Daojishi (context context, attributeset attrs) {super (context, Attrs);//TODO auto-generated Constructor Stublayoutinflater Inflater = (LayoutInflater) context.getsystemservice (context.layout_inflater_service); inflater.inflate (R.layout.daojishi, this); Initview (); inithandler (); initlisterner ();} Public Daojishi (context Context) {super (context);//TODO auto-generated constructor Stublayoutinflater inflater = ( Layoutinflater) context.getsystemservice (context.layout_inflater_service); inflater.inflate (R.layout.daojishi, this); initview (); inithandler (); initlisterner ();} public void Initview () {daojishi = (SmartButton9) Findviewbyid (r.id.daojishi_btn);} public void Initlisterner () {daojishi.setmyonclick (new onclicklistener () {@Overridepublic void OnClick (View v) {//TODO A Uto-generated method Stubif (daojishicallback! = Null) {if (daojishicallback. Start ()) {//mobile Number Verification passed, then continue the following Operation. } else {//mobile number verification failed, directly returned, do not perform the following Operation. return;}} Else {//callback interface is empty, there is a problem, return directly, do not perform the following Operation. return;} Daojishi.setclickable (false);//a Timer Timer is created when the button is pressed, timer = new Timer ();//create A timertask//timertask is an abstract class that implements the Runnable interface , so TimerTask is a child thread timertask timertask = new TimerTask () {Countdown alltime seconds, Default 50 seconds. int i = alltime; @Overridepublic void run () {//log.d ("debug", "thread on which the Run method is located:"//+ thread.currentthread (). getName ());//define A Message passed msg = new message (); Msg.what = i--;handler.sendmessage (msg); if (i < 0) {cancel ();}}};/ /define scheduled tasks, Depending on the parameters, you can do the following kinds of work://1. Schedule (timertask task, Date when) ー> perform a task at a fixed time//2. Schedule (timertask task, Date when, LONG//PERIOD) ー> repeats a task at a fixed time, and the repeat interval is controlled//3. Schedule (timertask task, Long Delay) ー> performs a task after a delay//4. Schedule (timertask task, Long delay, LONG//PERIOD) ー> repeatedly perform a task after a delay, the repetition interval controllable timer.schedule (timertask, 1, 1000);// 3 seconds to start countdown, countdown interval of 1 seconds}});} public void Inithandler () {handler = new handler () {@Overridepublic void handlemessage (Message msg) {//TODO auto-generate D method Stubsuper.handlemessage (msg);d Aojishi.settext (msg.what + "re-fetch after second"), if (daojishicallback! = null) { Daojishicallback.numchanged (msg.what);} if (msg.what = = 0) {daojishi.settext ("get authenticode");d aojishi.setclickable (true); If (daojishicallback! = null) { Daojishicallback. End();}}}};} public void Stop () {timer.cancel ();d aojishi.settext ("get authenticode");d aojishi.setclickable (true);} public void start () {daojishi.performclick ();} public void Setdaojishicallback (daojishicallback daojishicallback) {this.daojishicallback = daojishicallback;} public void Setalltime (int Alltime) {this.alltime = alltime;} /** * Countdown Control Callback interface for external Code. * * @author Admin * */public interface daojishicallback {/** * Click the button and then call the method before starting the Timer. * * @return returns True to start timing, false to exit Timing. */public Boolean Start ();/** * is Over. */public void End ();/** * number has Changed. * * @param num * @return */public void numchanged (int num);}}</pre>4 Engineering Downloads<p><p></p></p><p><p>http://download.csdn.net/detail/zhounanzhaode/8986391<br><br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original or translated articles, without Bo Master permission not Reproduced.</p></p> <p><p>Android send a verification code countdown button</p></p></span>

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.