finddreams:http://blog.csdn.net/finddreams/article/details/44174775
Registration when we often encounter, to the phone to send a verification code function, click to send a verification code, and then show the remaining number of seconds after resend verification code, as follows:
In order to achieve this effect, when the user clicks to send the verification code, shows the number of seconds left to resend, while setting the button's status is not clickable, so it is a gray background. When the countdown is over, set the button's status to a clickable state. The normal button in Android is not up to the requirements, we need to expand, so we need to customize a button, the code is as follows:
/** * @Description: Send the Verification code button, with a countdown, and in the process of transmission is not clickable; * Call Mode View.starttickwork () method; * @author Http://blog.csdn.net/finddreams * * Public class Sendvalidatebutton extends Button { Private Static Final intDisable_time = -;Private Static Final intMsg_tick =0;PrivateTimer Mtimer =NULL;PrivateTimerTask Mtask =NULL;Private intMdisabletime = Disable_time;//Countdown time, default 60 seconds Private intMenablecolor = Color.White;Private intMdisablecolor = Color.gray;PrivateString menablestring ="Get Verification Code";PrivateString mdisablestring ="remaining";PrivateString Second ="Seconds";Private BooleanMclickble =true;PrivateSendvalidatebuttonlistener Mlistener =NULL; Public int Getmdisabletime() {returnMdisabletime; } Public int Getmenablecolor() {returnMenablecolor; } Public void Setmenablecolor(intMenablecolor) { This. Menablecolor = Menablecolor; This. SetTextColor (Menablecolor); } Public int Getmdisablecolor() {returnMdisablecolor; } Public void Setmdisablecolor(intMdisablecolor) { This. Mdisablecolor = Mdisablecolor; } PublicStringgetmenablestring() {returnmenablestring; } Public Boolean isdisable() {if(Mdisabletime >0) {return true; }return false; } Public void setmenablestring(String menablestring) { This. menablestring = menablestring;if( This. menablestring! =NULL) { This. SetText (menablestring); } } PublicStringgetmdisablestring() {returnmdisablestring; } Public void setmdisablestring(String mdisablestring) { This. mdisablestring = mdisablestring; } Public void Setmlistener(Sendvalidatebuttonlistener Mlistener) { This. Mlistener = Mlistener; }PrivateHandler Mhandler =NewHandler (Looper.getmainlooper ()) {@Override Public void Handlemessage(Message msg) {Switch(msg.what) { CaseMsg_tick:tickwork (); Break;default: Break; }Super. Handlemessage (msg); } }; Public Sendvalidatebutton(context context, AttributeSet attrs) {Super(context, attrs); Initview (); }Private void Initview() { This. SetText (menablestring); This. setgravity (Gravity.center); This. SetTextColor (Menablecolor); Inittimer (); This. Setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {if(Mlistener! =NULL&& mclickble) {//Starttickwork ();Mlistener.onclicksendvalidatebutton (); } } }); }Private void Inittimer() {Mtimer =NewTimer (); }Private void Inittimertask() {Mtask =NewTimerTask () {@Override Public void Run() {mhandler.sendemptymessage (Msg_tick); } }; } Public void starttickwork() {if(mclickble) {mclickble =false; Sendvalidatebutton. This. SetText (mdisablestring + mdisabletime + Second); This. setenabled (false); Sendvalidatebutton. This. SetTextColor (Mdisablecolor); Inittimertask (); Mtimer.schedule (Mtask,0, +); } }/** * Call once per second */ Private void tickwork() {mdisabletime--; This. SetText (mdisablestring + mdisabletime + Second);if(Mlistener! =NULL) {Mlistener.ontick (); }if(Mdisabletime <=0) {stoptickwork (); } } Public void stoptickwork() {mtask.cancel (); Mtask =NULL; Mdisabletime = Disable_time; This. SetText (menablestring); This. SetTextColor (Menablecolor); This. setenabled (true); Mclickble =true; } Public interface sendvalidatebuttonlistener { Public void Onclicksendvalidatebutton(); Public void OnTick(); }}
然后我们只需要在用到地方引用一下即可:
<com .finddreams .view android:id= "@id/getcode_btn" Android:textcolor= "@color/white" android:layout_width= "wrap_content" android:layout_height= "match_parent" android:padding= "5dip" Android:background= "@drawable/bg_btn_shape_login" android:text= "Send verification code " android:textsize=" 16sp "/>
我们来看一下自定义Button的background的选择器bg_btn_shape_login.xml是如何做的:
<?xml version= "1.0" encoding= "Utf-8"?><selector xmlns:android="Http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"><shape android:shape="Rectangle"> <stroke android:width="1.0px" android:color="@color/line" / > <corners Android:bottomleftradius="5.0dip" Android:bottomrightradius ="5.0dip" android:topleftradius="5.0dip" Android:toprightradius ="5.0dip" /> <gradient android:angle="270.0" android:endcolor="#ff0e9379" Android:startcolor="#ff0e9379" /> </shape></Item> <item android:state_focused="true"><shape android:shape="Rectangle"> <gradient android:angle="270.0" android:endcolor="#ff0e9379" Android:startcolor="#ff0e9379" /> <stroke android:width="1.0px" android:color="@color/line" /> <corners Android:bottomleftradius="5.0dip" android:bottomrightradius= "5.0dip" Android:topleftradius="5.0dip" android:toprightradius="5.0dip" /> </shape></Item> <item android:state_enabled="false"><shape android:shape="Rectangle"> <gradient android:angle="270.0" android:endcolor="@color/gray_ Color " android:startcolor=" @color/gray_color " /> <stroke android:width="1.0px" android:color="@color/ Line " /> <corners Android:bottomleftradius="5.0dip" android:bottomrightradius= "5.0dip" Android:topleftradius="5.0dip" android:toprightradius="5.0dip" /> </shape></Item> <item> <shape android:shape="Rectangle"> <gradient android:angle="270.0" android:endcolor="@color/by56_bg" Android:startcolor="@color/by56_bg" /> <stroke android:width="1.0px" android:color="@color/line" /> <corners Android:bottomleftradius="5.0dip" android:bottomrightradius= "5.0dip" Android:topleftradius="5.0dip" android:toprightradius="5.0dip" /> </shape> </Item></selector>
这样就可以实现发送验证码的Button功能了,是不是很简单?当然应该还是有其他实现形式的,大家有兴趣的话可以自己去琢磨一下。
The button with the verification code that is commonly used to register in the app, with the countdown resend function