Android实现获取验证码的倒计时功能
开发中经常会遇到获取短信验证码,获取验证码后需要等待1分钟倒计时,这段时间是不能再次发送短信请求的。
实现如下:
CountDownTimerUtils.java
- package com.jackie.countdowntimer;
- import android.graphics.Color;
- import android.os.CountDownTimer;
- import android.text.Spannable;
- import android.text.SpannableString;
- import android.text.style.ForegroundColorSpan;
- import android.widget.TextView;
- /**
- * Created by Jackie on 2015/11/30.
- */
- public class CountDownTimerUtils extends CountDownTimer {
- private TextView mTextView;
- /**
- * @param textView The TextView
- *
- *
- * @param millisInFuture The number of millis in the future from the call
- * to {@link #start()} until the countdown is done and {@link #onFinish()}
- * is called.
- * @param countDownInterval The interval along the way to receiver
- * {@link #onTick(long)} callbacks.
- */
- 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); //设置按钮为灰色,这时是不能点击的
- /**
- * 超链接 URLSpan
- * 文字背景颜色 BackgroundColorSpan
- * 文字颜色 ForegroundColorSpan
- * 字体大小 AbsoluteSizeSpan
- * 粗体、斜体 StyleSpan
- * 删除线 StrikethroughSpan
- * 下划线 UnderlineSpan
- * 图片 ImageSpan
- * http://blog.****.net/ah200614435/article/details/7914459
- */
- SpannableString spannableString = new SpannableString(mTextView.getText().toString()); //获取按钮上的文字
- ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
- /**
- * public void setSpan(Object what, int start, int end, int flags) {
- * 主要是start跟end,start是起始位置,无论中英文,都算一个。
- * 从0开始计算起。end是结束位置,所以处理的文字,包含开始位置,但不包含结束位置。
- */
- 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); //还原背景色
- }
- }
使用:
CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(mButton, 60000, 1000);
mCountDownTimerUtils.start();
附上相关的资源文件:
bg_identify_code_normal.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="#FF9933" /> <!--填充色 透明-->
- <corners android:radius="8dp" /> <!-- 圆角 -->
- </shape>
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="#C0C0C0" /> <!--填充色 透明-->
- <corners android:radius="8dp" /> <!-- 圆角 -->
- </shape>
效果图如下:
另外,我发现个问题,就是如果我把上面的TextView换成Button的话,用SpannableString让倒计时时间字体变红是不生效的,具体原因是什么,我还没有找到,后续继续研究。
开发中经常会遇到获取短信验证码,获取验证码后需要等待1分钟倒计时,这段时间是不能再次发送短信请求的。
实现如下:
CountDownTimerUtils.java
- package com.jackie.countdowntimer;
- import android.graphics.Color;
- import android.os.CountDownTimer;
- import android.text.Spannable;
- import android.text.SpannableString;
- import android.text.style.ForegroundColorSpan;
- import android.widget.TextView;
- /**
- * Created by Jackie on 2015/11/30.
- */
- public class CountDownTimerUtils extends CountDownTimer {
- private TextView mTextView;
- /**
- * @param textView The TextView
- *
- *
- * @param millisInFuture The number of millis in the future from the call
- * to {@link #start()} until the countdown is done and {@link #onFinish()}
- * is called.
- * @param countDownInterval The interval along the way to receiver
- * {@link #onTick(long)} callbacks.
- */
- 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); //设置按钮为灰色,这时是不能点击的
- /**
- * 超链接 URLSpan
- * 文字背景颜色 BackgroundColorSpan
- * 文字颜色 ForegroundColorSpan
- * 字体大小 AbsoluteSizeSpan
- * 粗体、斜体 StyleSpan
- * 删除线 StrikethroughSpan
- * 下划线 UnderlineSpan
- * 图片 ImageSpan
- * http://blog.****.net/ah200614435/article/details/7914459
- */
- SpannableString spannableString = new SpannableString(mTextView.getText().toString()); //获取按钮上的文字
- ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
- /**
- * public void setSpan(Object what, int start, int end, int flags) {
- * 主要是start跟end,start是起始位置,无论中英文,都算一个。
- * 从0开始计算起。end是结束位置,所以处理的文字,包含开始位置,但不包含结束位置。
- */
- 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); //还原背景色
- }
- }
使用:
CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(mButton, 60000, 1000);
mCountDownTimerUtils.start();
附上相关的资源文件:
bg_identify_code_normal.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="#FF9933" /> <!--填充色 透明-->
- <corners android:radius="8dp" /> <!-- 圆角 -->
- </shape>
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="#C0C0C0" /> <!--填充色 透明-->
- <corners android:radius="8dp" /> <!-- 圆角 -->
- </shape>
效果图如下:
另外,我发现个问题,就是如果我把上面的TextView换成Button的话,用SpannableString让倒计时时间字体变红是不生效的,具体原因是什么,我还没有找到,后续继续研究。