Android使用CountDownTimer实现倒计时效果

所属分类: 软件编程 / Android 阅读数: 38
收藏 0 赞 0 分享

在开发中会经常用到倒计时这个功能,包括给手机发送验证码等等,之前我的做法都是使用Handler + Timer + TimerTask来实现,现在发现了这个类,果断抛弃之前的做法,相信还是有很多人和我一样一开始不知道Android已经帮我们封装好了一个叫CountDownTimer的类。

从字面上就可以看出来它叫倒数计时器又称定时器或计时器,采用Handler的方式实现,将后台线程的创建和Handler队列封装而成。
看了一下源码,发现这个类的调用还蛮简单,只有四个方法:

(1)public abstract void onTick(long millisUntilFinished);
固定间隔被调用

(2)public abstract void onFinish();
倒计时完成时被调用

(3)public synchronized final void cancel():
取消倒计时,当再次启动会重新开始倒计时

(4)public synchronized final CountDownTimer start():
启动倒计时

在这里可以看到前面两个是抽象方法,需要重写。

简单看一下代码:

package com.per.countdowntimer;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {
 private TextView mTvShow;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mTvShow = (TextView) findViewById(R.id.show);
 }

 /**
  * 取消倒计时
  * @param v
  */
 public void oncancel(View v) {
  timer.cancel();
 }

 /**
  * 开始倒计时
  * @param v
  */
 public void restart(View v) {
  timer.start();
 }

 private CountDownTimer timer = new CountDownTimer(10000, 1000) {

  @Override
  public void onTick(long millisUntilFinished) {
   mTvShow.setText((millisUntilFinished / 1000) + "秒后可重发");
  }

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

顺带附上XML布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/white"
 android:orientation="vertical"
 android:padding="16dp">

 <TextView
  android:id="@+id/show"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/hello_world" />

 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:onClick="restart"
  android:text="取消" />

 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:onClick="oncancel"
  android:text="结束" />

</LinearLayout>

最后说明一下:
CountDownTimer timer = new CountDownTimer(10000, 1000):以毫秒为单位,第一个参数是指从开始调用start()方法到倒计时完成的时候onFinish()方法被调用这段时间的毫秒数,也就是倒计时总的时间;第二个参数表示间隔多少毫秒调用一次 onTick方法,例如间隔1000毫秒。
在调用的时候直接使用timer.start();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多