美文网首页自定义控件
android 点击事件防抖 防止重复点击

android 点击事件防抖 防止重复点击

作者: 尘尘尘尘尘 | 来源:发表于2020-07-29 11:26 被阅读0次

    第一种
    继承OnClickListener

    import android.view.View;
    import android.view.View.OnClickListener;
    
    public abstract class OnClickFastListener implements OnClickListener {
        private static long lastClickTime;
        private long DELAY_TIME = 1000;
    
        public abstract void onFastClick(View view);
    
        private boolean isFastDoubleClick() {
            long time = System.currentTimeMillis();
            long timeD = time - lastClickTime;
            if (0 < timeD && timeD < this.DELAY_TIME) {
                return true;
            }
            lastClickTime = time;
            return false;
        }
    
        public void onClick(View v) {
            if (!isFastDoubleClick()) {
                onFastClick(v);
            }
        }
    
        public OnClickFastListener setLastClickTime(long delay_time) {
            this.DELAY_TIME = delay_time;
            return this;
        }
    }
    

    第二种
    点击时,动态的在点击控件上加上tag,存放时间戳

    import android.support.annotation.IntRange;
    import android.support.annotation.NonNull;
    import android.view.View;
    
    /**
     * @ProjectName: 
     * @Package: 
     * @ClassName: AntiShakeUtils
     * @Description: 防抖动点击
     * @Author: 
     * @CreateDate: 
     */
    
    public class AntiShakeUtils {
    
        private final static long INTERNAL_TIME = 1000;
    
        /**
         * Whether this click event is invalid.
         *
         * @param target target view
         * @return true, invalid click event.
         * @see #isInvalidClick(View, long)
         */
        public static boolean isInvalidClick(@NonNull View target) {
            return isInvalidClick(target, INTERNAL_TIME);
        }
    
        /**
         * Whether this click event is invalid.
         *
         * @param target       target view
         * @param internalTime the internal time. The unit is millisecond.
         * @return true, invalid click event.
         */
        public static boolean isInvalidClick(@NonNull View target, @IntRange(from = 0) long internalTime) {
            long curTimeStamp = System.currentTimeMillis();
            long lastClickTimeStamp = 0;
            Object o = target.getTag(R.id.last_click_time);
            if (o == null){
                target.setTag(R.id.last_click_time, curTimeStamp);
                return false;
            }
            lastClickTimeStamp = (Long) o;
            boolean isInvalid = curTimeStamp - lastClickTimeStamp < internalTime;
            if (!isInvalid)
                target.setTag(R.id.last_click_time, curTimeStamp);
            return isInvalid;
        }
    }
    

    使用

    mButton.setOnClickListener(view -> {
            if (AntiShakeUtils.isInvalidClick(view)) return;
            //TODO 
    });
    

    相关文章

      网友评论

        本文标题:android 点击事件防抖 防止重复点击

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