Android kotlin使用注解實(shí)現(xiàn)防按鈕連點(diǎn)功能的示例
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 // 檢測間隔時(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)功能的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法,結(jié)合實(shí)例形式分析了Android針對assets目錄下SQLite數(shù)據(jù)庫文件的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android自定義GestureDetector實(shí)現(xiàn)手勢ImageView
這篇文章主要為大家詳細(xì)介紹了Android自定義GestureDetector實(shí)現(xiàn)手勢ImageView的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android studio有關(guān)側(cè)滑的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android studio有關(guān)側(cè)滑的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Android中使用ViewStub實(shí)現(xiàn)布局優(yōu)化
ViewStub是Android布局優(yōu)化中一個(gè)很不錯(cuò)的標(biāo)簽/控件,直接繼承自View。雖然Android開發(fā)人員基本上都聽說過,但是真正用的可能不多。今天我們就來詳細(xì)探討下ViewStub的使用2016-09-09Android實(shí)現(xiàn)簡單的下拉阻尼效應(yīng)示例代碼
下面小編就為大家分享一篇Android實(shí)現(xiàn)簡單的下拉阻尼效應(yīng)示例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01實(shí)例詳解Android解決按鈕重復(fù)點(diǎn)擊問題
在項(xiàng)目中,由于網(wǎng)絡(luò)問題,不知道這個(gè)按鈕被點(diǎn)擊了幾次,為了防止這一問題發(fā)生,下面小編寫了一段實(shí)例代碼給大家詳解android解決按鈕重復(fù)點(diǎn)擊問題,對android按鈕重復(fù)點(diǎn)擊相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧2015-12-12Android 表情面板和軟鍵盤切換時(shí)跳閃問題的解決方法
這篇文章主要介紹了Android 表情面板和軟鍵盤切換時(shí)跳閃問題的解決方法,需要的朋友可以參考下2017-08-08Android編程經(jīng)典代碼集錦(復(fù)制,粘貼,瀏覽器調(diào)用,Toast顯示,自定義Dialog等)
這篇文章主要介紹了Android編程經(jīng)典代碼集錦,包括Android的復(fù)制,粘貼,瀏覽器調(diào)用,Toast顯示,自定義Dialog等實(shí)現(xiàn)技巧,非常簡單實(shí)用,需要的朋友可以參考下2016-01-01