A commonly used countdown tool for obtaining verification codes during registration and a verification code during registration

Source: Internet
Author: User

A commonly used countdown tool for obtaining verification codes during registration and a verification code during registration

 
// In this constructor, three parameters must be input: Activity and millisInFuture,
// One is the time for each reduction, and the last one is the countdown button.
Usage:
TimeLastUtil timeLastUtil = new TimeLastUtil (this, 60000,100 0, btn );
timeLastUtil.start();


Tool class:
 
Public class TimeLastUtil extends CountDownTimer {

Private Activity mActivity;
Private TextView btn; // button

// In this constructor, three parameters must be input: Activity and millisInFuture,
// One is countDownInterval, and the other is the button on which you want to upload the button.
Public TimeLastUtil (Activity mActivity, long millisInFuture,
Long countDownInterval, TextView btn ){
Super (millisInFuture, countDownInterval );
This. mActivity = mActivity;
This. btn = btn;
}



@ SuppressLint ("NewApi ")
@ Override
Public void onFinish (){
Btn. setText (R. string. register_regetchecknum );
Btn. setClickable (true); // obtain the Click again
Btn. setBackgroundColor (Color. parseColor ("#00 CCCC"); // restore the background Color
}

@ Override
Public void onTick (long millisUntilFinished ){
Btn. setClickable (false); // you cannot click
Btn. setText (millisUntilFinished/1000 + "s can be ressed"); // set the countdown time

// When the set button is gray, you cannot click it.
Btn. setBackgroundColor (Color. GRAY );
Spannable span = new SpannableString (btn. getText (). toString (); // get the text of the button
Span. setSpan (new ForegroundColorSpan (Color. RED), 0, 2,
Spannable. SPAN_INCLUSIVE_EXCLUSIVE); // The countdown time is displayed in red.
Btn. setText (span );

}

}
 

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.