android 中有一个封装好的CountDownTimer类,可以直接调用实现倒计时效果。

代码实现:

新建一个TimerCount类:

public class TimerCount extends CountDownTimer {private Button bnt;
//第一个参数代表总时长,第二个代表间隔时间,第三个是自己添加的参数,调用的时候传入需要的参数时使用public TimerCount(long millisInFuture, long countDownInterval, Button bnt) {super(millisInFuture, countDownInterval);this.bnt = bnt;}public TimerCount(long millisInFuture, long countDownInterval) {super(millisInFuture, countDownInterval);}
//倒计时结束后的处理,这里让button不可用且显示相应字符@Overridepublic void onFinish() {bnt.setEnabled(true);bnt.setText("获取验证码");}
//倒计时过程中的设置@SuppressLint("ResourceAsColor")@Overridepublic void onTick(long arg0) {// 设置倒计时过程中的字体属性bnt.setTextColor(R.color.divider);bnt.setText(arg0 / 1000 + "");bnt.setEnabled(false);}}

其他类对其调用:

@OnClick(R.id.get_code)void Code() {TimerCount timerCount = new TimerCount(60000, 1000, get_code);timerCount.start();}


更多相关文章

  1. Window窗口机制——WindowManager,ViewRootImpl,View理解
  2. Android(安卓)应用第一次运行时,引导页面的设置方法(只让程序Aciti
  3. android 学习四 加载图片
  4. Android用户界面 UI组件--TextView及其子类(三) EditView以及各
  5. Android(安卓)setContentView方法解析(一)
  6. android spinner自定义背景文字显示不全解决办法
  7. android 客户端数据传递之一:Web Service 代码
  8. Android(安卓)Studio 微信分享功能(包括可以分享到朋友圈,分享到朋
  9. 《第一行代码--Android》读书笔记之使用通知和多媒体

随机推荐

  1. 2011.04.14——— android 可伸缩的listv
  2. Android 实用开源项目集合 持续更新
  3. findViewById()
  4. android修改Zxing源码,避免出现错误信息
  5. Android联系人数据库全解析(3)
  6. Android 源码中编译Android studio工程
  7. Android音频播放
  8. [干货] Android 深入浅出 Activity 生命
  9. android 的 service远程运用
  10. Android(安卓)Studio老是提示重启ADB解决