android 手机验证码倒计时的实现

网络中有很多这样的需求。自己记录下来,仅作后期查看的一个记录

布局文件创建按钮

<Button
    android:id="@+id/btn_code"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:textColor="@color/text_black"
    android:text="获取验证码" />

自定义的时间统计类继承CountDownTimer 

public class TimeCount extends CountDownTimer {

    private Button btn_count;

    public TimeCount(long millisInFuture, long countDownInterval, Button btn_count) {
        super(millisInFuture, countDownInterval);
        this.btn_count = btn_count;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        btn_count.setEnabled(false);
        btn_count.setText(millisUntilFinished / 1000 + "秒");
    }

    @Override
    public void onFinish() {
        btn_count.setEnabled(true);
        btn_count.setText("获取验证码");

    }

}

activity的 oncreate中实例化

timeCount = new TimeCount(60000, 1000, btnCode);

public void onViewClicked(View view) 方法中调用
 
timeCount.start();
在点击登录的时候取消操作
 
//取消倒计时
timeCount.cancel();
timeCount.onFinish();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值