Android kotlin使用注解實現防按鈕連點功能

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) {

    }
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容