加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

【Android工具类】验证码倒计时帮助类CountDownButtonHelper的实

发布时间:2020-12-15 03:23:39 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 CountDownTimer timer = new CountDownTimer(AllTime,Intevel) { @Override public void onT

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

转载请注明出处: http://blog.csdn.net/zhaokaiqiang1992
    CountDownTimer timer = new CountDownTimer(AllTime,Intevel) {    
              
           @Override    
           public void onTick(long millisUntilFinished) {    
                  
           }    
              
           @Override    
           public void onFinish() {    
                
           }    
        };  

? new CountDownTimer(AllTime,Intevel)
    package com.example.countdowntimerdemo;  
      
    import android.os.CountDownTimer;  
    import android.util.Log;  
    import android.widget.Button;  
      
    /** 
     * 倒计时Button帮助类 
     *  
     * @author zhaokaiqiang 
     * @see http://blog.csdn.net/zhaokaiqiang1992 
     */  
    public class CountDownButtonHelper {  
      
        // 倒计时timer  
        private CountDownTimer countDownTimer;  
        // 计时结束的回调接口  
        private OnFinishListener listener;  
      
        private Button button;  
      
        /** 
         *  
         * @param button 
         *            需要显示倒计时的Button 
         * @param defaultString 
         *            默认显示的字符串 
         * @param max 
         *            需要进行倒计时的最大值,单位是秒 
         * @param interval 
         *            倒计时的间隔,单位是秒 
         */  
        public CountDownButtonHelper(final Button button,final String defaultString,int max,int interval) {  
      
            this.button = button;  
            // 由于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) {  
                    // 第一次调用会有1-10ms的误差,因此需要+15ms,防止第一个数不显示,第二个数显示2s  
                    button.setText(defaultString + "(" + ((time + 15) / 1000)  
                            + "秒)");  
                    Log.d("CountDownButtonHelper","time = " + (time) + " text = "  
                            + ((time + 15) / 1000));  
                }  
      
                @Override  
                public void onFinish() {  
                    button.setEnabled(true);  
                    button.setText(defaultString);  
                    if (listener != null) {  
                        listener.finish();  
                    }  
                }  
            };  
        }  
      
        /** 
         * 开始倒计时 
         */  
        public void start() {  
            button.setEnabled(false);  
            countDownTimer.start();  
        }  
      
        /** 
         * 设置倒计时结束的监听器 
         *  
         * @param listener 
         */  
        public void setOnFinishListener(OnFinishListener listener) {  
            this.listener = listener;  
        }  
      
        /** 
         * 计时结束的回调接口 
         *  
         * @author zhaokaiqiang 
         *  
         */  
        public interface OnFinishListener {  
            public void finish();  
        }  
      
    }  

? 测试源代码下载地址,请关注我的github https://github.com/ZhaoKaiQiang/CountDownTimerDemo

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读