Android kotlin使用注解實現(xiàn)防按鈕連點功能的示例
@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 { /** * 定義切點,標(biāo)記切點為所有被@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) {// 計算點擊間隔,沒有注解默認(rèn)500,有注解按注解參數(shù)來,注解參數(shù)為空默認(rèn)500;val singleClick = method.getAnnotation(SingleClick::class.java)val interval = singleClick.value// 檢測間隔時間是否達(dá)到預(yù)設(shè)時間并且線程空閑if (canClick(interval)) { joinPoint.proceed()} } else {joinPoint.proceed() } } catch (e: Exception) { // 出現(xiàn)異常不攔截點擊事件 joinPoint.proceed() } } // 判斷是否響應(yīng)點擊 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使用注解實現(xiàn)防按鈕連點功能的示例的詳細(xì)內(nèi)容,更多關(guān)于Android kotlin實現(xiàn)防按鈕連點功能的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. php bugs代碼審計基礎(chǔ)詳解2. html小技巧之td,div標(biāo)簽里內(nèi)容不換行3. xml中的空格之完全解說4. ASP中解決“對象關(guān)閉時,不允許操作。”的詭異問題……5. XML入門的常見問題(四)6. ASP中if語句、select 、while循環(huán)的使用方法7. ASP使用MySQL數(shù)據(jù)庫的方法8. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享9. WMLScript的語法基礎(chǔ)10. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法
