美文网首页
Android 添加Button的响应间隔

Android 添加Button的响应间隔

作者: 无敌未央様 | 来源:发表于2019-07-15 11:01 被阅读0次

    此工具类是Button每次点击之后都会有一个类似冷却时间的机制,防止在快速点击的情况做多次请求的局面

    public class ButtonUtils {
        private static long lastClickTime = 0;
        private static long DIFF = 1000;
        private static int lastButtonId = -1;
    
        /**
         * 判断两次点击的间隔,如果小于1000,则认为是多次无效点击
         *
         * @return
         */
        public static boolean isFastDoubleClick() {
            return isFastDoubleClick(-1, DIFF);
        }
    
        /**
         * 判断两次点击的间隔,如果小于1000,则认为是多次无效点击
         *
         * @return
         */
        public static boolean isFastDoubleClick(int buttonId) {
            return isFastDoubleClick(buttonId, DIFF);
        }
    
        /**
         * 判断两次点击的间隔,如果小于diff,则认为是多次无效点击
         *
         * @param diff
         * @return
         */
        public static boolean isFastDoubleClick(int buttonId, long diff) {
            long time = System.currentTimeMillis();
            long timeD = time - lastClickTime;
            if (lastButtonId == buttonId && lastClickTime > 0 && timeD < diff) {
                Log.v("isFastDoubleClick", "短时间内按钮多次触发");
                return true;
            }
            lastClickTime = time;
            lastButtonId = buttonId;
            return false;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Android 添加Button的响应间隔

          本文链接:https://www.haomeiwen.com/subject/rnigkctx.html