Android 发送验证码倒计时常用的两种办法

在项目开发中,在用户注册界面,修改密码等界面会出现发送验证码的情况。这时候就会遇到倒计时的情况了,如图:


我经常用到的有两种办法:

第一种:Timer
/**
 * Description:自定义Timer
 * <p>
 * Created by Mjj on 2016/12/4.
 */

public class TimeCount extends CountDownTimer {

    private Button button;

    //参数依次为总时长,和计时的时间间隔
    public TimeCount(Button button, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.button = button;
    }

    //计时过程显示
    @Override
    public void onTick(long millisUntilFinished) {
        String time = "(" + millisUntilFinished / 1000 + ")秒";
        setButtonInfo(time, "#c1c1c1", false);
    }

    //计时完毕时触发
    @Override
    public void onFinish() {
        setButtonInfo("重新获取", "#f95353", true);
    }

    /**
     * 验证按钮在点击前后相关设置
     *
     * @param content 要显示的内容
     * @param color   颜色值
     * @param isClick 是否可点击
     */
    private void setButtonInfo(String content, String color, boolean isClick) {
        button.setText(content);
        button.setBackgroundColor(Color.parseColor(color));
        button.setClickable(isClick);
    }
}

说明:根据自己的需求,在这里修改背景颜色和不同状态显示文字即可,在需要监听的按钮下直接调用new TimerCount(xxx,xxx,xxx).start()即可。
 
第二种:handler
/**
     * 第二种方式:使用Handler
     * <p>
     * 静态内部类:避免内存泄漏
     */
    private static class MyHandler extends Handler {

        private final WeakReference<MainActivity> weakReference;

        public MyHandler(MainActivity activity) {
            weakReference = new WeakReference<MainActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            MainActivity activity = weakReference.get();
            if (activity != null) {
                switch (msg.what) {
                    case 0:
                        if (msg.arg1 == 0) {
                            btn2.setText("重新获取");
                            btn2.setBackgroundColor(Color.parseColor("#f95353"));
                            btn2.setClickable(true);
                        } else {
                            btn2.setText("(" + msg.arg1 + ")秒");
                            btn2.setBackgroundColor(Color.parseColor("#c1c1c1"));
                            btn2.setClickable(false);
                        }
                        break;
                }
            }
        }
    }

    /**
     * 监听按钮下直接调用即可
     */
    private void sendMessageClick() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 59; i >= 0; i--) {
                    Message msg = myHandler.obtainMessage();
                    msg.arg1 = i;
                    myHandler.sendMessage(msg);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

说明:此种方式采用的handler实时接收消息来设置Button的状态,对于消息的发送用的是sendMessage方式,也可以使用post方式。


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值