关于android:Android-kotlin使用注解实现防按钮连点功能

29次阅读

共计 2542 个字符,预计需要花费 7 分钟才能阅读完成。

SingleClick:

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class SingleClick(
    // 点击间隔时间,毫秒
    val value: Long = 500
)

SingleClickAspect:

import android.os.SystemClock
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

@Aspect
class SingleClickAspect {

    /**
     * 定义切点,标记切点为所有被 @SingleClick 注解的办法
     * 留神:这里 me.baron.test.annotation.SingleClick 须要替换成
     * 你本人我的项目中 SingleClick 这个类的全门路哦
     */
    @Pointcut("execution(@你的包名.SingleClick * *(..))")
    fun methodAnnotated() {}

    /**
     * 定义一个切面办法,包裹切点办法
     */
    @Around("methodAnnotated()")
    @Throws(Throwable::class)
    fun aroundJoinPoint(joinPoint: ProceedingJoinPoint) {
        try {
            // 取出办法的注解
            val signature = joinPoint.signature as MethodSignature
            val method = signature.method
            // 查看办法是否有注解
            val hasAnnotation = method != null && method.isAnnotationPresent(SingleClick::class.java)
            if (hasAnnotation) {
                // 计算点击距离,没有注解默认 500,有注解按注解参数来,注解参数为空默认 500;val singleClick = method.getAnnotation(SingleClick::class.java)
                val interval = singleClick.value
                // 检测间隔时间是否达到预设工夫并且线程闲暇
                if (canClick(interval)) {joinPoint.proceed()
                }
            } else {joinPoint.proceed()
            }
        } catch (e: Exception) {
            // 出现异常不拦挡点击事件
            joinPoint.proceed()}
    }

    // 判断是否响应点击
    private fun canClick(interval: Long): Boolean {val time = SystemClock.elapsedRealtime()
        val timeInterval  = Math.abs(time - mLastClickTime)
        if (timeInterval > interval) {
            mLastClickTime = time
            return true
        }
        return false
    }

    companion object {
        // 最初一次点击的工夫
        private var mLastClickTime: Long = 0
    }

}

build.gradle(我的项目):

buildscript {
    dependencies {classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4'}
}

build.gradle(APP):

plugins {id 'android-aspectjx'}

应用:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:onClick="onTextClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    @SingleClick(800)
    fun onTextClick(view: View) {}}

正文完
 0