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注解的方法
   * 注意:这里 你的包名.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实现防按钮连点功能的资料请关注我们其它相关文章!

(0)

相关推荐

  • 利用Kotlin实现破解Android版的微信小游戏--跳一跳

    前言 昨天下午,微信小程序开放了游戏接口,朋友圈瞬间炸开了锅,尤其是"跳一跳"这款游戏的成绩单,在朋友圈刷了一波又一波. 下面就来给大家介绍了关于Kotlin破解Android版的微信小游戏跳一跳的相关内容,让大家可以好好炫耀一番. 成果 跳一跳 微信小程序可以玩游戏了,我们来破解一下<跳一跳>这个官方出品的小游戏吧. 思路 用usb调试安卓手机,用adb截图并用鼠标测量距离,然后计算按压时间后模拟按压. $ adb shell input swipe <x1>

  • Android Studio Kotlin代码和java代码相互转化实例

    1.java转成kotlin 方法1:快捷键:Ctrl+Shift+Alt+K 方法2:Code - Convert Java File To Kotlin File 2.kotlin转成java 1.Tools>Kotlin>Show Kotlin Bytecode 2.点击 Decompile 补充知识:Android Studio Kotlin插件的简单使用 1.新建一个Project "Kotlin" ,然后在app的build.gradle文件中添加配置,如图所示

  • 在Android中如何使用DataBinding详解(Kotlin)

    前言 本问主要介绍DataBinding在Android App中的使用方法.数据绑定是将"提供器"的数据源与"消费者"绑定并使其同步的一种通用技术. 1. Android应用程序使用数据绑定 1.1 介绍DataBinding Android通过DataBinding提供了编写声明型布局的支持.这样可以最大程度简化布局和逻辑相关联的代码. 数据绑定要求修改文件,外层需要包裹一个layout布局.主要通过@{} 或 @={}语法把布局中的元素和表达式的引用写入到属性

  • Android结合kotlin使用coroutine的方法实例

    最近入了Android坑,目前还处于疯狂学习的状态,所以很久都没有写博客了.今天记录一个小代码片段,在Android上使用coroutine 的小例子. 由于我自己是做一个记账软件来学习的,我用了gRPC,最开始我是使用线程来做网络请求的: thread { // 网络请求代码 runOnUiThread { // 更新UI的代码 } } 今天把这一套全部重写成用coroutine. 首先coroutine得有个调度器,英文叫做 "Dispatchers",有这么几个: Dispatc

  • Android kotlin+协程+Room数据库的简单使用

    Room Room是Google为了简化旧版的SQLite操作专门提供的 1.拥有了SQLite的所有操作功能 2.使用简单(类似于Retrofit),通过注解的方式实现相关功能.编译时自动生成实现类impl 3.LiveData,LifeCycle,Paging天然融合支持 导入 ... plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-android-extensions' id 'kotlin-kap

  • Android Studio / IDEA kotlin 显示 var 真实类型操作

    File -> Settings -> Editor -> Inlay Hints -> Kotlin 勾选 Show local variable type hints 启用前 启用后 补充知识:Android Studio 编译: Program type already present: XXX 解决方案 情况1:个例 build.gradle 中 dependencies { classpath 'com.android.tools.build:gradle:3.1.1'

  • 利用Kotlin如何实现Android开发中的Parcelable详解

    坑 先来看看 Android Studio 给的自动实现. 新建一个数据类,让它实现 Parcelable data class Worker( var id: Int, var name: String, var tasks: MutableList<Int> ) : Parcelable 使用 Android Studio 自带的 Add Parcelable Implementation ,然后你就得到了... data class Worker( var id: Int, var na

  • Android使用Kotlin实现多节点进度条

    时间轴主要使用Recyclerview来实现.开发语言使用Kotlin,代码相对java少了许多也简洁许多 源代码下载地址 效果图: MainActivity.kt package com.example.lee.linenode import android.annotation.SuppressLint import android.os.Build import android.support.v7.app.AppCompatActivity import android.os.Bundl

  • Android Studio kotlin生成编辑类注释代码

    更新了AS 3.1.2之后,发现新建Kotlin类,类注释依然木有,没办法只有自己动手了. 方法很简单,编辑File Header就可以啦. 只需要编辑自己想要的模板就可以啦. /** * @Author ${USER} * @Date ${DATE}-${TIME} * @Email chrisSpringSmell@gmail.com */ 支持的动态命令不多,只有一些简单的命令. 新建类效果: 补充知识:Android Studio javadoc 生成注释文档 相信大家刚开始写代码的时候

  • Android Kotlin环境使用ButterKnife的方法

    Butter Knife 黄油刀大家应该都挺熟悉的,有这个之后,就不用写一堆的findViewById,体力活,最近试着玩玩Kotlin语言,也就尝试在Kotlin语言环境下使用ButterKnife,有一点小问题,解决并分享一下. 先看看java环境的用法 1.安装插件,然后重启Android studio. 安装插件.jpg 2.使用,点击一下在setContentView(R.layout.activity_main);然后快捷键Alt+insert. Alt+insert.jpg 3.使

  • Android studio 生成带Kotlin文档的实现方式

    首先才项目的build.gradle 加入classpath 'org.jetbrains.dokka:dokka-android-gradle-plugin:0.9.16' (0.9.16是当前版本) dependencies { classpath 'org.jetbrains.dokka:dokka-android-gradle-plugin:0.9.15' } 然后再module的build.gradle中加入apply plugin: 'org.jetbrains.dokka-andr

随机推荐