Android实现获取验证码的倒计时功能

╰半夏微凉° 2022-05-16 08:42 347阅读 0赞

开发中经常会遇到获取短信验证码,获取验证码后需要等待1分钟倒计时,这段时间是不能再次发送短信请求的。

实现如下:

CountDownTimerUtils.java

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

使用:

  1. CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(mTextView, 60000, 1000);
  2. mCountDownTimerUtils.start();
  3. public void b1(View view) {
  4. //发送
  5. SMSSDK.getVerificationCode("86", edt1.getText().toString());
  6. phone = edt1.getText().toString();
  7. // 倒计时
  8. CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(mTextView, 60000, 1000);
  9. mCountDownTimerUtils.start();
  10. }

XML:

70

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:orientation="vertical"
  6. android:layout_height="match_parent">
  7. <EditText
  8. android:id="@+id/editText"
  9. android:layout_width="match_parent"
  10. android:maxLength="11"
  11. android:inputType="number"
  12. android:hint="请输入11位手机号码"
  13. android:layout_height="50dp" />
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:orientation="horizontal">
  18. <EditText
  19. android:id="@+id/editText2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="50dp"
  22. android:layout_weight="3" />
  23. <TextView
  24. android:id="@+id/mTextView"
  25. android:layout_width="wrap_content"
  26. android:layout_height="50dp"
  27. android:layout_weight="1"
  28. android:background="@color/colorPrimary"
  29. android:gravity="center_horizontal|center_vertical"
  30. android:onClick="b1"
  31. android:text="获取验证码"
  32. android:textColor="#fff"
  33. android:textSize="18sp" />
  34. </LinearLayout>
  35. <Button
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:onClick="b2"
  39. android:text="点击验证" />
  40. </LinearLayout>

发表评论

表情:
评论列表 (有 0 条评论,347人围观)

还没有评论,来说两句吧...

相关阅读