Verification Code countdown in android, android Verification Code

Source: Internet
Author: User
Tags getcolor

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.

 

  

 

Related Article

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.