Verification Code countdown in android, android Verification Code
1. Implement the countdown effect of a Verification Code
2. Implementation
In the figure, the verification code is a button.
The key part is to declare a TimeCount that inherits from CountDownTimer.
/* Verification Code countdown */
Private class TimeCount extends CountDownTimer {
/**
* @ Param millisInFuture total time length (MS)
* @ Param countDownInterval: interval (MS). The onTick method is called every time interval.
*/
Public TimeCount (long millisInFuture, long countDownInterval ){
Super (millisInFuture, countDownInterval );
}
@ Override
Public void onTick (long millisUntilFinished) {// countdown status
GetVerificationCodeBtn. setClickable (false); // you cannot click to set the button.
GetVerificationCodeBtn. setBackground (getResources (). getDrawable (R. drawable. get_verification_code_waitting_bg); // modify the background of the button
GetVerificationCodeBtn. setTextColor (getResources (). getColor (R. color. black); // modify the textColor of the button
GetVerificationCodeBtn. setText (millisUntilFinished/1000 + "s can be ressed"); // display the countdown Text of the button
}
@ Override
Public void onFinish () {// countdown end status
GetVerificationCodeBtn. setBackground (getResources (). getDrawable (R. drawable. login_btn_bg ));
GetVerificationCodeBtn. setTextColor (getResources (). getColor (R. color. white ));
GetVerificationCodeBtn. setClickable (true); // reset the button to clickable.
GetVerificationCodeBtn. setText ("Get again"); // modify the text of the button
}
}
Finally, declare TimeCount in the Code and instantiate it. In the button click event, call the. start () method to start the timer.
TimeCount timeCount = new TimeCount (60000,1000 );
TimeCount. start ();
So far.