Android发送验证码倒计时自定义View

要点

1.通过计时器Timer TimerTask 设置每秒执行一次递减任务

2.通过handler 在UI线程中修改秒数

3.设置开关,对Timer TimerTask 停止消除任务

4.设置初始按钮样式,和倒计时按钮样式。(自行设置)

以下是自定义View代码

import android.os.Handler;
import android.util.AttributeSet;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 倒计时时间控件
 * Created by d on 2016/9/13.
 */
public class TimeButton extends Button {
    private long length = 60 * 1000;// 倒计时长度,这里给了默认60秒
    private String text_after = "s";
    private String text_before = "获取验证码";


    private Timer timer;
    private TimerTask timerTask;
    private long time;

    public TimeButton(Context context) {
        super(context);
    }

    public TimeButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TimeButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 倒计时
     */
   private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            TimeButton.this.setText(time / 1000 + text_after);
            time -= 1000;
            if (time < 0) {
                TimeButton.this.setEnabled(true);
                TimeButton.this.setText(text_before);
                clearTimer();
            }
        }
    };

    private void initTimer() {
        time = length;
        if(timer==null) {
            timer = new Timer();
        }
        if(timerTask==null) {
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    handler.sendEmptyMessage(0x01);
                }
            };
        }
    }

    private void clearTimer() {
        try {
            if (timerTask != null) {
                timerTask.cancel();
                timerTask = null;
            }
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
            //还原样式
            setBackgroundResource(R.drawable.shape_btn_round_corner_orange);
            setTextColor(getResources().getColor(R.color.common_background_white));
        }catch (Exception e){

        }
    }

    /**
     * 开始倒计时
     */
    public void start_count_down(){
        this.setEnabled(false);
        initTimer();
        timer.schedule(timerTask, 0, 1000);
        //设置倒计时样式
        setBackgroundResource(R.drawable.shape_common_radius_corner_line);
        setTextColor(getResources().getColor(R.color.common_top_bar_color_in_orange));
    }

    /**
     * 停止倒计时
     */
    public void stop_count_down(){
        clearTimer();
    }


    /**
     * 设置到计时长度
     *
     * @param length
     *            时间 默认毫秒
     * @return
     */
    public TimeButton setLength(long length) {
        this.length = length;
        return this;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值