CountDownTimerUtils
public class CountDownTimerUtils extends CountDownTimer { private TextView mTextView; public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); this.mTextView = textView; } @Override public void onTick(long millisUntilFinished) { mTextView.setClickable(false); mTextView.setText(millisUntilFinished / 1000 + "秒"); mTextView.setBackgroundColor(Color.parseColor("#cccccc")); SpannableString spannableString = new SpannableString(mTextView.getText().toString()); ForegroundColorSpan span = new ForegroundColorSpan(Color.RED); spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); mTextView.setText(spannableString); } @Override public void onFinish() { mTextView.setText("获取验证码"); mTextView.setClickable(true); mTextView.setBackgroundColor(Color.parseColor("#FF4081")); }
使用
CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tvCode, 60000, 1000); mCountDownTimerUtils.start();