SingleClick:
@Retention(AnnotationRetention.RUNTIME)@Target(AnnotationTarget.FUNCTION)annotation class SingleClick( // 点击间隔时间,毫秒 val value: Long = 500)
SingleClickAspect:
import android.os.SystemClockimport org.aspectj.lang.ProceedingJoinPointimport org.aspectj.lang.annotation.Aroundimport org.aspectj.lang.annotation.Aspectimport org.aspectj.lang.annotation.Pointcutimport org.aspectj.lang.reflect.MethodSignature@Aspectclass SingleClickAspect { /** * 定义切点,标记切点为所有被@SingleClick注解的方法 * 注意:这里 你的包名.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) { }}
以上就是Android kotlin使用注解实现防按钮连点功能的示例的详细内容,更多关于Android kotlin实现防按钮连点功能的资料请关注其它相关文章!