Android 处理 View 重复点击的 4 种方式

本文介绍四种防止Android应用中重复点击按钮的方法:抽象类、代理模式、RxAndroid之RxBinding及AOP之EclipseAspectJ,并详细展示了每种方法的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、抽象类

//实现 View.OnClickListener 接口
public abstract class OnThrottleClickListener implements View.OnClickListener {
    private static final String TAG = "OnThrottleClickListener";
    private static final int SKIP_DURATION = 300;//milliseconds
    private long mLastClickTime;
    @Override
    public void onClick(View v) {
        if (System.currentTimeMillis() - mLastClickTime > SKIP_DURATION) {
            onThrottleClick(v);
            mLastClickTime = System.currentTimeMillis();
        } else {
            Log.e(TAG, "OnThrottleClickListener: 重复点击");
        }
    }

    protected abstract void onThrottleClick(View v);
}
//代替 new View.OnClickListener() 使用
id_tv_1.setOnClickListener(new OnThrottleClickListener() {
     @Override
     protected void onThrottleClick(View v) {
       Log.e(TAG, "onClick: OnThrottleClickListener ");
     }
});

2、代理模式

//代理类实现 View.OnClickListener 接口
public class ThrottleClickProxy implements View.OnClickListener {
    private static final String TAG = "ThrottleClickProxy";
    private static final int SKIP_DURATION = 300;//milliseconds
    private long mLastClickTime;
    //源对象
    private View.OnClickListener mOriginListener;
	//构造
    public ThrottleClickProxy(View.OnClickListener mOriginListener) {
        this.mOriginListener = mOriginListener;
    }
    @Override
    public void onClick(View v) {
        if (System.currentTimeMillis() - mLastClickTime >= SKIP_DURATION) {
            mOriginListener.onClick(v);
            mLastClickTime = System.currentTimeMillis();
        } else {
            Log.e(TAG, "ThrottleClickProxy: 重复点击");
        }
    }
}
//使用 
id_tv_2.setOnClickListener(new ThrottleClickProxy(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
                Log.e(TAG, "onClick: ThrottleClickProxy ");
          }
   }));

3、RxAndroid 之 RxBinding

implementation 'com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha1'
 RxView.clicks(id_tv_3)
       .throttleFirst(300, TimeUnit.MILLISECONDS)
       .subscribe(new Consumer<Unit>() {
          @Override
          public void accept(Unit unit) throws Exception {
              Log.e(TAG, "onClick: throttleFirst ");
          }
     });

4、AOP 之 Eclipse AspectJ

//采用 AspectJX 来快速配置 Eclipse AspectJ
//project
dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        //add
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}
plugins {
    id 'com.android.application'
    //add
    id 'android-aspectjx'
}
//application module
dependencies {
    //add
    implementation 'org.aspectj:aspectjrt:1.9.5'
}
@Aspect
public class ThrottleClickAspect {
    private static final String TAG = "ThrottleClickAspect";
    private static final int SKIP_DURATION = 3000;
    private long mLastClickTime;

    //所有的 android.view.View.OnClickListener.onClick 方法都会被织入,像第三方组件 RxView.clicks() 里也会
    @Around("execution(* android.view.View.OnClickListener.onClick(..))")
    public void aroundViewOnClick(ProceedingJoinPoint joinPoint) throws Throwable {
        if (System.currentTimeMillis() - mLastClickTime >= SKIP_DURATION) {
            joinPoint.proceed();
            mLastClickTime = System.currentTimeMillis();
        } else {
            Log.e(TAG, "ThrottleClickAspect: 重复点击");
        }
    }
}
  • 代码无侵入方式,直接生效了

通过注解明确指定

  • 增加 @ThrottleClick 注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ThrottleClick {
    int skipDuration() default 1000;
}
  • 修改 ThrottleClickAspect 类
@Aspect
public class ThrottleClickAspect {
    private static final String TAG = "ThrottleClickAspect";
    private static final int SKIP_DURATION = 300;
    //    private int mLastClickViewId;
    private long mLastClickTime;
    //声明是注解标记切点
	//包路径
    @Pointcut("execution(@com.louisgeek.aop.ThrottleClick * *(..))")
    public void pointcutThrottleClick() {
        Log.e(TAG, "pointcutThrottleClick: ");
    }
	//
    @Around("pointcutThrottleClick()")
    public void aroundThrottleClick(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(ThrottleClick.class)) {
            return;
        }
        ThrottleClick throttleClick = method.getAnnotation(ThrottleClick.class);
        //
        if (throttleClick == null) {
            return;
        }
        int skipDuration = throttleClick.skipDuration();
        if (skipDuration < 0) {
            skipDuration = SKIP_DURATION;
        }
        if (System.currentTimeMillis() - mLastClickTime >= skipDuration) {
            //继续执行
            joinPoint.proceed();
            mLastClickTime = System.currentTimeMillis();
        } else {
            Log.e(TAG, "aroundThrottleClick: 重复点击");
        }
    }

}
//使用  
id_tv_4.setOnClickListener(new View.OnClickListener() {
           //注解标记
            @ThrottleClick
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: @ThrottleClick ");
            }
  });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

louisgeek

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值