Get the countdown function of verification code

Posted by tsabar on Tue, 31 Mar 2020 21:54:26 +0200

The countdown of obtaining verification code is very common in current apps. Its main function point is to set a click event for TextView. However, when the countdown occurs after clicking, clicking cannot trigger the click event.

The click event can be triggered again when the countdown is over and the verification code is retrieved;

In real projects, it's usually set for one minute. Let's not set it for that long. Set it for 10 seconds;

First of all, my demo is very simple. The tool class is copied directly to the project without any care. It only needs two steps and two lines of code:

Step 1: initialize the tool class association and implement the countdown TextView

/** 
 * First parameter: TextView control (need to implement countdown TextView) 
 * Second parameter: total countdown time, in milliseconds; 
 * The third parameter: gradient event, at least 1 second, that is to say, setting 0-1000 is to change the gradient time in one second, and setting more than 1000 changes the gradient time 
 * The fourth parameter: background before clicking textview 
 * The fifth parameter: background after clicking textview 
 */  
SendSmsTimerUtils mCountDownTimerUtils = new SendSmsTimerUtils(textView, 10000, 1000,R.color.colorAccent,R.color.abcd);

Step 2: start countdown in TextView's click event

textView.setOnClickListener(new View.OnClickListener() {  

    public void onClick(View v) {  

        mCountDownTimerUtils.start();  
    }  
}); 

Let's look down at the encapsulation of tool classes:
This is a tool class found on github by accident. It feels very good. It encapsulates the following and uses it directly:

public class SendSmsTimerUtils extends CountDownTimer {  
    private int inFuture;  
    private int downInterval;  
    private TextView mTextView;  

    public SendSmsTimerUtils(TextView textView, long millisInFuture, long countDownInterval, int inFuture, int downInterval) {  
        super(millisInFuture, countDownInterval);  
        this.mTextView = textView;  
        this.inFuture=inFuture;  
        this.downInterval=downInterval;  
    }  

    public void onTick(long millisUntilFinished) {  
        mTextView.setClickable(false);  
        mTextView.setText(millisUntilFinished / 1000 + "Seconds to resend");  
        mTextView.setBackgroundResource(downInterval);   

        SpannableString spannableString = new SpannableString(mTextView.getText().toString());  
        ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);  
        //Set seconds to red  
        if (millisUntilFinished/1000 > 9) {  
            spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);  
        } else {  
            spannableString.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);  
        }  
        mTextView.setText(spannableString);  
    }  

    @Override  
    public void onFinish() {  
        mTextView.setText("Get the verification code again");  
        mTextView.setClickable(true);  
        mTextView.setBackgroundResource(inFuture);  
    }  
} 

Record
Reprint address: http://blog.csdn.net/zheng_jiao/article/details/52097910

Topics: github