倒计时工具类

时间:2022-07-25
本文章向大家介绍倒计时工具类,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

CountDownTimer

如果你赶时间就是直接复制文章最后的工具类粘贴到你的项目中使用即可

在APP开发过程中我们通常会有一个获取手机验证码这样的一个操作,其中有一个倒计时 一般来说就是60s,倒计时期间不可点击 ,计时结束之后重新获取验证码。如下图所示

如图所示这样子,就是倒计时的实现过程了。我们来看一下这个工具类的使用方法

			@BindView(R.id.get_code)
    		TextView getCode;
    		
			CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(getCode, 60000, 1000);
            countDownTimerUtils.start();

getCode就是你用来上图中的 获取验证码的控件名称,60000毫秒就是60秒 1000毫秒就是1秒 ,总时长60s 开始倒计时 一般来说是用button,但是,我这里用的是TextView(个人喜好) ,当我点击时就会进行倒计时,然后文字会变成倒计时。

工具类如下所示:

public class CountDownTimerUtils extends CountDownTimer {
    WeakReference<TextView> tvCodeWr;//控件软引用,防止内存泄漏
    CountDownTimer timer;

    /**
     * @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);
        tvCodeWr = new WeakReference<>(textView);
        timer = this;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        TextView mTextView = tvCodeWr.get();
        if (mTextView != null) {
            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.csdn.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);
        } else {
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
        }
    }

    @Override
    public void onFinish() {
        TextView mTextView = tvCodeWr.get();
        if (mTextView != null) {
            mTextView.setText("获取验证码");
            mTextView.setClickable(true);//重新获得点击
        } else {
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
        }
    }

}

这个工具类,可以复制粘贴直接使用的,但需要导一些包进去,都是As中的,没有第三方的包。