最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android kotlin使用注解實(shí)現(xiàn)防按鈕連點(diǎn)功能的示例

 更新時(shí)間:2021年03月13日 11:50:24   作者:頭發(fā)還沒禿  
這篇文章主要介紹了Android kotlin使用注解實(shí)現(xiàn)防按鈕連點(diǎn)功能的示例,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下

SingleClick:

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class SingleClick(
  // 點(diǎn)擊間隔時(shí)間,毫秒
  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 {

  /**
   * 定義切點(diǎn),標(biāo)記切點(diǎn)為所有被@SingleClick注解的方法
   * 注意:這里 你的包名.SingleClick 需要替換成
   * 你自己項(xiàng)目中SingleClick這個(gè)類的全路徑
   */
  @Pointcut("execution(@你的包名.SingleClick * *(..))")
  fun methodAnnotated() { }

  /**
   * 定義一個(gè)切面方法,包裹切點(diǎn)方法
   */
  @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) {
        // 計(jì)算點(diǎn)擊間隔,沒有注解默認(rèn)500,有注解按注解參數(shù)來,注解參數(shù)為空默認(rèn)500;
        val singleClick = method.getAnnotation(SingleClick::class.java)
        val interval = singleClick.value
        // 檢測(cè)間隔時(shí)間是否達(dá)到預(yù)設(shè)時(shí)間并且線程空閑
        if (canClick(interval)) {
          joinPoint.proceed()
        }
      } else {
        joinPoint.proceed()
      }
    } catch (e: Exception) {
      // 出現(xiàn)異常不攔截點(diǎn)擊事件
      joinPoint.proceed()
    }
  }

  // 判斷是否響應(yīng)點(diǎn)擊
  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 {
    // 最后一次點(diǎn)擊的時(shí)間
    private var mLastClickTime: Long = 0
  }

}

build.gradle(項(xiàng)目):

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使用注解實(shí)現(xiàn)防按鈕連點(diǎn)功能的示例的詳細(xì)內(nèi)容,更多關(guān)于Android kotlin實(shí)現(xiàn)防按鈕連點(diǎn)功能的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • android 開發(fā) 文件讀寫應(yīng)用案例分析

    android 開發(fā) 文件讀寫應(yīng)用案例分析

    在Android應(yīng)用中保存文件會(huì)使用到文件讀寫技術(shù),本文將詳細(xì)介紹,需要的朋友可以參考下
    2012-12-12
  • 分析Android中線程和線程池

    分析Android中線程和線程池

    我們知道線程是CPU調(diào)度的最小單位。在Android中主線程是不能夠做耗時(shí)操作的,子線程是不能夠更新UI的。在Android中,除了Thread外,扮演線程的角色有很多,如AsyncTask,IntentService和HandlerThread等等。本文將介紹Android中線程和線程池。
    2021-06-06
  • android 判斷橫豎屏問題的詳解

    android 判斷橫豎屏問題的詳解

    本篇文章是對(duì)android中如何判斷橫豎屏的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Android ListView實(shí)現(xiàn)上拉加載更多和下拉刷新功能

    Android ListView實(shí)現(xiàn)上拉加載更多和下拉刷新功能

    這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)上拉加載更多和下拉刷新功能,介紹了ListView刷新原理及實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android編程連接MongoDB及增刪改查等基本操作示例

    Android編程連接MongoDB及增刪改查等基本操作示例

    這篇文章主要介紹了Android編程連接MongoDB及增刪改查等基本操作,簡(jiǎn)單介紹了MongoDB功能、概念、使用方法及Android操作MongoDB數(shù)據(jù)庫的基本技巧,需要的朋友可以參考下
    2017-07-07
  • Android view繪制流程詳解

    Android view繪制流程詳解

    View 的繪制是在 ViewRoot 的 performTraversals() 開始的,它歷經(jīng) measure(測(cè)量), layout(布局), draw(繪制) 三個(gè)流程將 View 顯示在屏幕上。
    2021-05-05
  • Android開發(fā)框架MVC-MVP-MVVM-MVI的演變Demo

    Android開發(fā)框架MVC-MVP-MVVM-MVI的演變Demo

    這篇文章主要為大家介紹了Android開發(fā)框架MVC-MVP-MVVM-MVI的演變Demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Android使用線程獲取網(wǎng)絡(luò)圖片的方法

    Android使用線程獲取網(wǎng)絡(luò)圖片的方法

    這篇文章主要為大家詳細(xì)介紹了Android使用線程獲取網(wǎng)絡(luò)圖片的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android通過交互實(shí)現(xiàn)貝塞爾曲線的繪制

    Android通過交互實(shí)現(xiàn)貝塞爾曲線的繪制

    本篇我們將介紹簡(jiǎn)單的交互式繪圖,通過獲取觸控位置來設(shè)定貝塞爾曲線的控制點(diǎn),從而實(shí)現(xiàn)交互式繪制曲線,感興趣的小伙伴可以了解一下
    2022-05-05
  • Android ListView彈性效果的實(shí)現(xiàn)方法

    Android ListView彈性效果的實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了Android ListView彈性效果的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下
    2016-05-05

最新評(píng)論

耿马| 桓台县| 呈贡县| 巴南区| 项城市| 通海县| 红原县| 石阡县| 新安县| 沭阳县| 柘荣县| 肃南| 礼泉县| 康马县| 玉环县| 诏安县| 德惠市| 麦盖提县| 稷山县| 阿坝| 隆安县| 东港市| 九龙城区| 纳雍县| 大城县| 汉中市| 句容市| 阜康市| 庄河市| 广东省| 厦门市| 宾阳县| 怀仁县| 原阳县| 岑巩县| 安西县| 抚松县| 那坡县| 太白县| 喀喇沁旗| 宝清县|