android 实现倒计时

package com.dejun.commonsdk.util;

import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import java.lang.ref.WeakReference;

/**
 * Author:DoctorWei
 * Time:2018/12/22 14:57
 * Description:
 * email:1348172474@qq.com
 */

public class CountDownTimerUtil {
    private TextView downTimeTv;//倒计时按钮
    public static final int MSG_CODE = 100;//消息标识
    public static final long WHOLE_TIME = 60000l;//倒计时总时间 单位秒
    public static final long INTERNAL_TIME = 1000l;//倒计时间隔时间
    public static final String RESET_SEND = "重新发送";
    public static final String AFTER_SECONDS = "秒后";
    private long millisInFuture;
    private long countDownInterval;
    private WeakReference<TextView> weakReference;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int what = msg.what;
            if (what == MSG_CODE) {
                if (millisInFuture > 0) {//倒计时 不可点击
                    showTvEnable(false);
                    millisInFuture -= countDownInterval;
                    if (weakReference.get() != null) {
                        sendEmptyMessageDelayed(MSG_CODE, countDownInterval);
                    }
                } else {//结束倒计时
                    showTvEnable(true);
                }
            }
        }
    };

    private void showTvEnable(boolean tvEnable) {
        TextView textView = weakReference.get();
        if (textView != null) {
            textView.setEnabled(tvEnable);
            if (tvEnable) {
                textView.setText(RESET_SEND);
                if (tvEnableFalseTvColor != 0) {
                    textView.setTextColor(tvEnableFalseTvColor);
                }
                if (tvEnableFalseBgColor != 0) {
                    textView.setBackgroundResource(tvEnableFalseBgColor);
                }
            } else {
                textView.setText(millisInFuture / 1000 + AFTER_SECONDS + RESET_SEND);
                if (tvEnableTrueTvColor != 0) {
                    textView.setTextColor(tvEnableTrueTvColor);
                }
                if (tvEnableTrueBgColor != 0) {
                    textView.setBackgroundResource(tvEnableTrueBgColor);
                }

            }
        }
    }

    public CountDownTimerUtil(TextView downTimeTv) {
        this(downTimeTv, WHOLE_TIME, INTERNAL_TIME);
    }

    public CountDownTimerUtil(TextView downTimeTv, long millisInFuture, long countDownInterval) {
        this.downTimeTv = downTimeTv;
        weakReference = new WeakReference<TextView>(downTimeTv);
        this.millisInFuture = millisInFuture;
        this.countDownInterval = countDownInterval;
    }

    public CountDownTimerUtil startCountDownTime() {
        handler.removeMessages(MSG_CODE);
        handler.sendEmptyMessage(MSG_CODE);
        return this;
    }
    int tvEnableTrueTvColor;
    int tvEnableFalseTvColor;
    int tvEnableTrueBgColor;
    int tvEnableFalseBgColor;

    public CountDownTimerUtil intColor(int tvEnableTrueTvColor, int tvEnableFalseTvColor, int tvEnableTrueBgColor, int tvEnableFalseBgColor) {
        this.tvEnableTrueTvColor = tvEnableTrueTvColor;
        this.tvEnableFalseTvColor = tvEnableFalseTvColor;
        this.tvEnableTrueBgColor = tvEnableTrueBgColor;
        this.tvEnableFalseBgColor = tvEnableFalseBgColor;
        return this;
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值