Android CountDownTimer倒计时器的使用和Handler
来源:互联网 发布:淘宝客服转化率怎么算 编辑:程序博客网 时间:2024/06/14 22:51
http://blog.csdn.net/lilu_leo/article/details/6941724
/** * 验证码请求倒计时工具类 * * @version v1.0 * @copyright 2010-2015 */@SuppressLint("ResourceAsColor")public class TimeUtil { // 倒计时timer private CountDownTimer countDownTimer; // 计时结束的回调接口 private OnFinishListener listener; private TextView tv; private Context mContext; /** * @param tvSend 需要显示倒计时的TextView * @param max 需要进行倒计时的最大值,单位是秒 * @param interval 倒计时的间隔,单位是秒 */ public TimeUtil(final Context context, final TextView tvSend, int max, int interval) { this.mContext = context; this.tv = tvSend; // 由于CountDownTimer并不是准确计时,在onTick方法调用的时候,time会有1-10ms左右的误差,这会导致最后一秒不会调用onTick() // 因此,设置间隔的时候,默认减去了10ms,从而减去误差。 // 经过以上的微调,最后一秒的显示时间会由于10ms延迟的积累,导致显示时间比1s长max*10ms的时间,其他时间的显示正常,总时间正常 countDownTimer = new CountDownTimer(max * 1000, interval * 1000 - 10) { @Override public void onTick(long time) { tv.setTextColor(context.getResources().getColor(R.color.black)); tv.setBackgroundColor(context.getResources().getColor(R.color.white)); // 第一次调用会有1-10ms的误差,因此需要+15ms,防止第一个数不显示,第二个数显示2s tv.setText(((time + 15) / 1000) + "秒后可重发"); } @Override public void onFinish() { tv.setClickable(true); // tv.setTextColor(Color.parseColor("#028ce3"));//tv.setText(R.string.repeat_send); if (listener != null) { listener.finish(); } } }; } /** * 开始倒计时 */ public void start() { tv.setClickable(false); countDownTimer.start(); } /** * 设置倒计时结束的监听器 * * @param listener */ public void setOnFinishListener(OnFinishListener listener) { this.listener = listener; } /** * 计时结束的回调接口 */ public interface OnFinishListener { public void finish(); } public void cancel() { countDownTimer.cancel(); tv.setClickable(true); // tv.setTextColor(Color.parseColor("#028ce3")); tv.setText(mContext.getResources().getString(R.string.repeat_send)); }}
handler使用倒计时
/*=====*/ Handler msmTimeOpHandler = new Handler(); Runnable msmTimeOpRunnable = new Runnable() { @Override public void run() { msmTimeOp(); } }; private void msmTimeOp() { smsVailTime--; if (smsVailTime == 1) { vTimeTv.setText("正在跳转"); boolean isfirstStart = mSP.get().getBoolean(Config.SP_ISFIRSTSTART, false); if (isfirstStart) { PrefManager.getInstance().setLong(ExtraConstrat.OP_TIME, 0); startActivity(MainActivity.class); } else { startActivity(GuidActivity.class); } mActivity.finish(); } else { vTimeTv.setText(smsVailTime + "秒跳转"); msmTimeOpHandler.postDelayed(msmTimeOpRunnable, 1000); } }
发送按钮监听执行
msmTimeOpHandler.postDelayed(msmTimeOpRunnable, 1000);
阅读全文
0 0
- Android CountDownTimer倒计时器的使用和Handler
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android CountDownTimer倒计时器的使用
- Android倒计时器CountDownTimer的简单使用
- Python自学笔记
- swift的闭包结构
- Linux 远程登录管理工具—Host-Only 连接
- 字体号数与像素对应关系
- 计算机组成原理学习笔记(1)
- Android CountDownTimer倒计时器的使用和Handler
- [JAVA] 配置Tomcat JNDI数据源
- XMind甘特图之使用方法第二篇
- uva 12169 ex_gcd lrj—P316
- Linux内核网络分层结构
- unity 修改编辑器
- BZOJ 1077 天平 (差分约束)
- 如何使用万能地图下载器解决百度地图的偏移问题
- C++虚函数与纯虚函数