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 );
}
}