本文实例为大家分享了Android实现点击获取验证码60秒后重新获取的具体代码,供大家参考,具体内容如下
上代码
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.setBackgroundResource(R.drawable.bg_identify_code_press); //设置按钮为灰
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.setBackgroundResource(R.drawable.bg_identify_code_normal);
}
}
TextView背景色
bg_identify_code_press
bg_identify_code_normal
布局代码
项目代码
private void initView() {
tv_yzm = (TextView) findViewById(R.id.tv_yzm);
tv_yzm.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tv_yzm, 60000, 1000);
mCountDownTimerUtils.start();
}
});
}
效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



