android防重复点击

android防重复点击

简介

此方法侵入性小,使用方便,代码量少,基于AspectJ实现
先看使用方式

    @SingleClick(2000)
    @Override
    public void onClick(View view) {
    	super.onClick(view)
    }

使用只需要在点击事件上面添加SingleClick(防重复点击时间)注解就好,是不是非常方便!

代码实现

1.加入AspectJ插件
在你工程的build.gradle里面加入

dependencies {
		//noinspection GradleDependency
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.0'
}

在你的module的build.gradle加入

apply plugin: 'com.android.application'
apply plugin: 'android-aspectjx'
dependencies {
	implementation 'org.aspectj:aspectjrt:1.8.9'
}

2.单击事件代码
先定义一个注解SingleClick

/**
 * author:  zhouchaoxiang
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SingleClick {
    long value() default 500;//默认的点击间隔时间
}

编写判断单击逻辑代码

import android.view.View;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.Method;

/**
 * author:  zhouchaoxiang
 */
@Aspect
public class SingleClickAspect {
	/**
     * 最近一次点击的时间
     */
    private static long mLastClickTime;
    /**
     * 最近一次点击的控件ID
     */
    private static int mLastClickViewId;
    
    @Pointcut("execution(@SingleClick 的全路径(如:@com.demo.www.demo.annotation.SingleClick * *(..)))")
    public void methodAnnotated() {}

    @Around("methodAnnotated()")
    public void aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
        // 取出方法的参数
        View view = null;
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof View) {
                view = (View) arg;
                break;
            }
        }
        if (view == null) {
            return;
        }
        // 取出方法的注解
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        if (!method.isAnnotationPresent(SingleClick.class)) {
            return;
        }
        SingleClick singleClick = method.getAnnotation(SingleClick.class);
        // 判断是否快速点击
        if (!isFastDoubleClick(view, singleClick.value())) {
            // 不是快速点击,执行原方法
            joinPoint.proceed();
        }
    }
    /**
     * 是否是快速点击
     * @param v  点击的控件
     * @param intervalMillis  时间间期(毫秒)
     * @return  true:是,false:不是
     */
    public boolean isFastDoubleClick(View v, long intervalMillis) {
        int viewId = v.getId();
        long time = System.currentTimeMillis();
        long timeInterval = Math.abs(time - mLastClickTime);
        if (timeInterval < intervalMillis && viewId == mLastClickViewId) {
            return true;
        } else {
            mLastClickTime = time;
            mLastClickViewId = viewId;
            return false;
        }
    }
}

到这里整个逻辑就完成了,赶紧试试吧

猜你喜欢

转载自blog.csdn.net/qq_39191116/article/details/83652115