原文链接:Android获取手机验证码之倒计时功能实现 - 简书
1、创建倒计时工具类:
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 + " s"); //设置倒计时时间 // mTextView.setBackgroundResource(R.drawable.bg_identify_code_press); //设置按钮为灰色,这时是不能点击的 SpannableString spannableString = new SpannableString(mTextView.getText().toString()); //获取按钮上的文字 ForegroundColorSpan span = new ForegroundColorSpan(Color.GRAY); spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为灰色 mTextView.setText(spannableString); } @Override public void onFinish() { mTextView.setText("重新获取验证码"); mTextView.setTextColor(Color.parseColor("#DCAD79")); mTextView.setClickable(true);//重新获得点击 // mTextView.setBackgroundResource(R.drawable.bg_identify_code_normal); //还原背景色 } }
2、在activity中调用
sendCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CountDownTimerUtils count = new CountDownTimerUtils(sendCode, 30000, 1000); count.start(); } });