android中的倒计时
16lz
2021-12-04
在Android中比如获取验证码的时候,都会有一个倒计时的按钮,提示多少秒之后重发。这种情况我们就可以使用CountDownTimer这个类,来进行倒计时的操作。
class MyCountDownTimer extends CountDownTimer { public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { mTimeBtn.setText("获取验证码"); } @Override public void onTick(long millisUntilFinished) { mTimeBtn.setText(millisUntilFinished / 1000 + "秒后重发"); } }
这里我们需要重写构造方法和onfinish()、onTick(),构造方法中的long millisInFuture代表倒计时的总时间,long
countDownInterval代表倒计时的间隔时间。onFinsh()是在倒计时结束后的操作。onTick()是执行倒计时过程中的操作。
使用时:
<span style="font-size:14px;">MyCountDownTime mDownTimer = new MyCountDownTimer(60000, 1000); mDownTimer.start();</span>
更多相关文章
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
- 【代码】android 通过GPS获取用户地理位置并监听位置变化
- Android根据图片Uri获取图片绝对路径
- android 应用嵌入 admob 广告
- android中实现截屏的三种思路
- Android选择框
- android 随笔
- 如何在Android设备中用NDK编译SQLite并且对SQLite进行操作(增删)-H
- android 想要弹出另外界面操作步骤