我们在开发中经常用到倒计时的功能,比如发送验证码后,倒计时60s再进行验证码的获取,为了方便以后使用,这里做个记录,讲讲倒计时器的实现。

1、先进行倒计时工具类的封装

  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. /**
  20. * 倒计时期间会调用
  21. * @param millisUntilFinished
  22. */
  23. @Override
  24. public void onTick(long millisUntilFinished) {
  25. mTextView.setClickable(false); //设置不可点击
  26. mTextView.setText(millisUntilFinished / 1000 + "秒"); //设置倒计时时间
  27. mTextView.setBackgroundResource(R.drawable.shape_verify_btn_press); //设置按钮为灰色,这时是不能点击的
  28.  
  29. SpannableString spannableString = new SpannableString(mTextView.getText().toString()); //获取按钮上的文字
  30. ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
  31. /**
  32. * public void setSpan(Object what, int start, int end, int flags) {
  33. * 主要是start跟end,start是起始位置,无论中英文,都算一个。
  34. * 从0开始计算起。end是结束位置,所以处理的文字,包含开始位置,但不包含结束位置。
  35. */
  36. spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为红色
  37. mTextView.setText(spannableString);
  38. }
  39.  
  40. /**
  41. * 倒计时完成后调用
  42. */
  43. @Override
  44. public void onFinish() {
  45. mTextView.setText("重新获取验证码");
  46. mTextView.setClickable(true);//重新获得点击
  47. mTextView.setBackgroundResource(R.drawable.shape_verify_btn_normal); //还原背景色
  48. }
  49. }

让这个工具类继承CountDownTimer,然后把显示倒计时的View传进去,在倒计时期间会调用onTick方法,在这个方法里面对View的倒计时界面进行刷新,倒计时结束后,会调用onFinish方法,在里面恢复View的状态即可。

2、布局文件

未点击获取验证码时的view布局 shape_verify_btn_normal.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle" >
  4.  
  5. <!-- 填充的颜色:这里设置背景透明 -->
  6. <solid android:color="@color/maincolor" />
  7. <!-- 边框的颜色 :不能和窗口背景色一样-->
  8. <stroke
  9. android:width="1dp"
  10. android:color="@color/white" />
  11. <!-- 设置按钮的四个角为弧形 -->
  12. <!-- android:radius 弧形的半径 -->
  13. <corners android:radius="5dip" />
  14.  
  15. <!-- padding:Button里面的文字与Button边界的间隔 -->
  16. <padding
  17. android:bottom="5dp"
  18. android:left="5dp"
  19. android:right="5dp"
  20. android:top="5dp" />
  21. </shape>

点击获取验证码之后的view布局  shape_verify_btn_press.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle" >
  4.  
  5. <!-- 填充的颜色:这里设置背景透明 -->
  6. <solid android:color="@color/graywhite" />
  7. <!-- 边框的颜色 :不能和窗口背景色一样-->
  8. <stroke
  9. android:width="1dp"
  10. android:color="@color/white" />
  11. <!-- 设置按钮的四个角为弧形 -->
  12. <!-- android:radius 弧形的半径 -->
  13. <corners android:radius="5dip" />
  14.  
  15. <!-- padding:Button里面的文字与Button边界的间隔 -->
  16. <padding
  17. android:bottom="5dp"
  18. android:left="5dp"
  19. android:right="5dp"
  20. android:top="5dp" />
  21. </shape>

整个界面的布局

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/login_bg">
  6.  
  7. <EditText
  8. android:id="@+id/rst_phone_number"
  9. android:layout_width="match_parent"
  10. android:layout_height="40dp"
  11. android:inputType="phone"
  12. android:hint="@string/phone_number"
  13. android:textSize="16sp"
  14. android:textColor="@color/black"
  15. android:singleLine="true"
  16. android:background="@drawable/shape_form"
  17. android:textCursorDrawable="@drawable/text_cursor"
  18. android:layout_marginLeft="30dp"
  19. android:layout_marginRight="30dp"
  20. android:layout_marginTop="30dp"
  21. android:layout_gravity="center_vertical"/>
  22.  
  23. <LinearLayout
  24. android:layout_width="match_parent"
  25. android:layout_height="40dp"
  26. android:layout_marginLeft="30dp"
  27. android:layout_marginRight="30dp"
  28. android:layout_marginTop="15dp"
  29. android:orientation="horizontal">
  30.  
  31. <EditText
  32. android:id="@+id/rst_verify_code"
  33. android:layout_width="wrap_content"
  34. android:layout_height="match_parent"
  35. android:layout_weight="1"
  36. android:inputType="number"
  37. android:hint="@string/rst_verify_code"
  38. android:textSize="16sp"
  39. android:textColor="@color/black"
  40. android:singleLine="true"
  41. android:background="@drawable/shape_form"
  42. android:textCursorDrawable="@drawable/text_cursor" />
  43.  
  44. <TextView
  45. android:id="@+id/rst_send_code"
  46. android:layout_width="130dp"
  47. android:layout_height="match_parent"
  48. android:text="@string/rst_send_code"
  49. android:textSize="13sp"
  50. android:textColor="@color/white"
  51. android:gravity="center"
  52. android:layout_marginLeft="10dp"
  53. android:background="@drawable/shape_verify_btn_normal"/>
  54.  
  55. </LinearLayout>
  56.  
  57. <Button
  58. android:id="@+id/rst_next_step"
  59. android:layout_width="match_parent"
  60. android:layout_height="40dp"
  61. android:layout_margin="30dp"
  62. android:text="@string/rst_next_step"
  63. android:textSize="15sp"
  64. android:textColor="@color/white"
  65. android:background="@drawable/shape_btn"/>
  66.  
  67. </LinearLayout>

这里布局有个问题,因为获取验证码这个TextView中的字会在倒计时期间有变化,而且每次字的变化长度不一样,如果把它的layout_width设为wrap_content,那么这个TextView的长度会变化,影响界面美观,所以可以把它的长度固定,然后把验证码输入框的layout_weight设为1,这样就可以了。

3、具体使用方法

  1. // 发送成功进入倒计时
  2. countDownTimer = new CountDownTimerUtils(tv_send_code, 60000, 1000);
  3. countDownTimer.start();

其中60000代表要倒计时的时长,即60s;1000代表每次递减1s。

返回
顶部