Android Get SMS Verification Code Countdown

Source: Internet
Author: User

At present, more and more applications in the registration or the corresponding operation, the request for SMS Verification code, click on the button to get SMS verification code, is the countdown, such as Countdown 120S, during the countdown period, the button click is invalid, when the countdown is over, if you did not get to the verification code, you can click again. There are many ways to achieve the countdown, and we will implement it today by inheriting the Android.os.CountDownTimer class!

First look at our package of Countdown tool class, mainly in order to use in many places, using a number of construction methods, is to use more flexible, as long as the incoming logarithm can be called:

public class Mycounttimer extends Countdowntimer {
public static final int time_count = 121000;//time prevents display starting from 119s (with Countdown 120s for example)
private TextView btn;
private int endstrrid;
private int normalcolor, timingcolor;//non-timed text color, text color during timing

/**
* Parameters millisinfuture          Total countdown time (e.g. 60s,120s etc.)
* Parameters Countdowninterval     Gradient time (1s )

        * parameters btn               click button ( Because the button is a TextView subclass, for general purpose my parameter is set to TextView)

* parameter Endstrrid after the countdown is over, the button corresponds to the text displayed
*/
Public Mycounttimer (Long millisinfuture, long countdowninterval, TextView btn, int endstrrid) {
Super (Millisinfuture, countdowninterval);
THIS.BTN = BTN;
This.endstrrid = Endstrrid;
}


/**

* Parameters above have comments
*/
PublicMycounttimer(TextView btn, int endstrrid) {
Super (Time_count, 1000);
THIS.BTN = BTN;
This.endstrrid = Endstrrid;
}

PublicMycounttimer(TextView btn) {
Super (Time_count, 1000);
THIS.BTN = BTN;
This.endstrrid = r.string.txt_getmsgcode_validate;
}


PublicMycounttimer(TextView tv_varify, int normalcolor, int timingcolor) {
This (tv_varify);
This.normalcolor = NormalColor;
This.timingcolor = Timingcolor;
}

Trigger when timing is complete
@Override
public void OnFinish () {
if (NormalColor > 0) {
Btn.settextcolor (NormalColor);
}
Btn.settext (Endstrrid);
Btn.setenabled (TRUE);
}

Timing Process Display
@Override
public void OnTick (long millisuntilfinished) {
if (Timingcolor > 0) {
Btn.settextcolor (Timingcolor);
}
Btn.setenabled (FALSE);
Btn.settext (millisuntilfinished/1000 + "s");
}
}

Then you can use it on the page where you want to implement the countdown:

Like the button to hit the time in Acvititya.

Button Smsbtn=findviewbyid (r.id ...) ;

mycounttimertimecount = newmycounttimer(smsbtn, 0xfff30008, 0xff969696);//Incoming text color value
Timecount.start ();

If you do not pass in the color value, you can also set the color according to the button state in the layout file of the click button Smsbtn.

<button
Android:id= "@+id/rebind_sms_btn"
Android:layout_width= "120DP"
android:layout_height= "45DP"
android:layout_marginleft= "5DP"
android:layout_marginright= "5DP"
Android:background= "@null"
android:gravity= "Center"
android:text= "Get SMS Verification Code"
Android:textcolor= "@color/hkb_binder_phone_text_color"
Android:textsize= "16sp"/>

The XML file that corresponds to the text color:

<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_enabled= "false" android:color= "#969696"/>
<item android:color= "#f30008"/>
</selector>

The effect is the same. Code OK, I am more lazy, do not post!

Android Get SMS Verification Code Countdown

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.