Android中注册获取验证码倒计时按钮

  1. public class CountDownTimerUtils extends CountDownTimer {  
  2.     private TextView mTextView;  
  3.   
  4.     /** 
  5.      * @param textView          The TextView 
  6.      * 
  7.      * 
  8.      * @param millisInFuture    The number of millis in the future from the call 
  9.      *                          to {@link #start()} until the countdown is done and {@link #onFinish()} 
  10.      *                          is called. 
  11.      * @param countDownInterval The interval along the way to receiver 
  12.      *                          {@link #onTick(long)} callbacks. 
  13.      */  
  14.     public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {  
  15.         super(millisInFuture, countDownInterval);  
  16.         this.mTextView = textView;  
  17.     }  
  18.   
  19.     @Override  
  20.     public void onTick(long millisUntilFinished) {  
  21.         mTextView.setClickable(false); //设置不可点击  
  22.         mTextView.setText(millisUntilFinished / 1000 + "秒后可重新发送");  //设置倒计时时间  
  23.         mTextView.setBackgroundResource(R.drawable.bg_identify_code_press); //设置按钮为灰色,这时是不能点击的  
  24.   
  25.         /** 
  26.          * 超链接 URLSpan 
  27.          * 文字背景颜色 BackgroundColorSpan 
  28.          * 文字颜色 ForegroundColorSpan 
  29.          * 字体大小 AbsoluteSizeSpan 
  30.          * 粗体、斜体 StyleSpan 
  31.          * 删除线 StrikethroughSpan 
  32.          * 下划线 UnderlineSpan 
  33.          * 图片 ImageSpan 
  34.          * http://blog.csdn.net/ah200614435/article/details/7914459 
  35.          */  
  36.         SpannableString spannableString = new SpannableString(mTextView.getText().toString());  //获取按钮上的文字  
  37.         ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);  
  38.         /** 
  39.          * public void setSpan(Object what, int start, int end, int flags) { 
  40.          * 主要是start跟end,start是起始位置,无论中英文,都算一个。 
  41.          * 从0开始计算起。end是结束位置,所以处理的文字,包含开始位置,但不包含结束位置。 
  42.          */  
  43.         spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为红色  
  44.         mTextView.setText(spannableString);  
  45.     }  
  46.   
  47.     @Override  
  48.     public void onFinish() {  
  49.         mTextView.setText("重新获取验证码");  
  50.         mTextView.setClickable(true);//重新获得点击  
  51.         mTextView.setBackgroundResource(R.drawable.bg_identify_code_normal);  //还原背景色  
  52.     }  

 

使用:

CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(mButton, 60000, 1000);

mCountDownTimerUtils.start();



posted @ 2017-10-30 14:54  kim_liu  阅读(1308)  评论(0编辑  收藏  举报