Android倒计时的三种实现方式

引言

目前,处于安全的考虑,大部分软件账户注册都需要短信验证码,而点击验证码之后倒计时获取变得习以为常,下面个人总结了三种实现倒计时的方法。供大家参考:

1 . 谷歌原生的计时器(CountDownTimer类)
首先,由于谷歌的开发者官网对国内开放了,所以我们先去看一下关于CountDownTimer的解释和应用:

Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field:

//案例
 new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

The calls to onTick(long) are synchronized to this object so that one call to onTick(long) won't ever occur before the previous callback is complete. This is only relevant when the implementation of onTick(long) takes an amount of time to execute that is significant compared to the countdown interval.

然而对于我这种四级没过的人来说理解是:同过设置构造函数中的两个参数,第一个是倒计时需要的时间,第二个是时间的间隔,在该函数中含有两个回调函数onTick(long)onFinish(),第一个回到函数回传过来剩余的时间,第二个是倒计时完成时回调的方法。下面我把我写的代码贴出来(这里我写的倒计时是六秒钟):

      countDownTimer = new CountDownTimer(6000, 1000) {
            int n = 6;

            @Override
            public void onTick(long l) {
                Log.e(TAG, "onTick: " + l);
                 tv_time.setClickable(false);
                tv_time.setText(
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值