Objective
This article mainly introduces the text message verification code function, here summed up two common ways, can be used directly.
Look at the picture
Timer
Description: here in time starting from 10, is to demonstrate the time not to wait too long and modified.
Methods are as follows
1, the first way: Timer
/** * Description: Custom timer * <p> * Created by MJJ on 2016/12/4.
* * public class Timecount extends Countdowntimer {private button button; The arguments are in total length, and the timing interval is public timecount (Button button, long millisinfuture, long Countdowninterval) {super (Millisinfut
Ure, Countdowninterval);
This.button = button; The//Timing process displays @Override public void Ontick (long millisuntilfinished) {String time = "(+ MILLISUNTILFINISHED/1
000 + ") seconds";
SetButtonInfo (Time, "#c1c1c1", false);
The @Override public void OnFinish () {SetButtonInfo ("regain", "#f95353", true) is triggered when the timer is completed; /** * Verification button before and after the click of the relevant settings * * @param content to display * @param color color value * @param isclick can be clicked/Priva
te void SetButtonInfo (string content, String color, Boolean Isclick) {button.settext (content);
Button.setbackgroundcolor (Color.parsecolor (Color));
Button.setclickable (Isclick); }
}
Description: according to their own needs, here to modify the background color and different status display text, in the need to listen to the button directly call new Timercount (XXX,XXX,XXX). Start ().
2, the second way: Handler
/** * Second way: Use Handler * <p> * static inner class: Avoid memory leaks/private static class MyHandler extends Handler {p
Rivate final weakreference<mainactivity> WeakReference;
Public MyHandler (mainactivity activity) {weakreference = new weakreference<mainactivity> (activity);
@Override public void Handlemessage (msg) {super.handlemessage);
Mainactivity activity = Weakreference.get (); if (activity!= null) {switch (msg.what) {case 0:if (msg.arg1 = = 0) {btn2.
SetText ("Re-acquisition");
Btn2.setbackgroundcolor (Color.parsecolor ("#f95353"));
Btn2.setclickable (TRUE);
else {btn2.settext ("+ Msg.arg1 +") seconds);
Btn2.setbackgroundcolor (Color.parsecolor ("#c1c1c1"));
Btn2.setclickable (FALSE);
} break; /** * Direct call under the * Listening button * * * private void SendMessageClick () {new Runnable () {@Override public void run () {for (int i = i >= 0;
-) {Message msg = Myhandler.obtainmessage ();
MSG.ARG1 = i;
Myhandler.sendmessage (msg);
try {thread.sleep (1000);
catch (Interruptedexception e) {e.printstacktrace ();
}}}). Start (); }
Note: This method adopts the handler real-time receive message to set the button state, for the message sent by the SendMessage method, you can also use the Post method.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, if you have questions you can message exchange.