@Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.FUNCTION) annotation class SingleClick( // 点击间隔时间,毫秒 val value: LOng= 500 )
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注解的方法 * 注意:这里 你的包名.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 } }
buildscript { dependencies { classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4' } }
plugins { id 'android-aspectjx' }
<&#63;xml version="1.0" encoding="utf-8"&#63;>
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle&#63;) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } @SingleClick(800) fun onTextClick(view: View) { } }
以上就是Android kotlin使用注解实现防按钮连点功能的示例的详细内容,更多关于Android kotlin实现防按钮连点功能的资料请关注其它相关文章!