From aaef14cfac53c2bfb9a1ebbf72367f492ec3b586 Mon Sep 17 00:00:00 2001 From: zhujiang521 <57114034@qq.com> Date: Wed, 29 Apr 2020 09:21:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E4=BA=86=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E5=8C=BA=E5=A4=A7=E5=AE=B6=E6=8F=90=E5=88=B0=E7=9A=84=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++ .../zj/it/bhne/androidaop/MainActivity.kt | 2 + .../java/zj/it/bhne/androidaop/OnClick.kt | 46 +++++++++---------- build.gradle | 1 + singclick/build.gradle | 7 +++ .../src/main/java/com/zj/singclick/OnClick.kt | 37 +++++++++++++++ 6 files changed, 78 insertions(+), 23 deletions(-) create mode 100644 singclick/src/main/java/com/zj/singclick/OnClick.kt diff --git a/README.md b/README.md index 2f37b03..fa39552 100644 --- a/README.md +++ b/README.md @@ -79,3 +79,11 @@ implementation 'com.github.zhujiang521:AndroidAOP:1.0.1' ``` OK。 + +新增了Kotlin的判断重复的方法,大家可以按照下面的方法直接进行调用,比写注解更加灵活方便: +``` +btnKuoZhan.setSafeListener { + ToastUtils.showShort("222") + Log.e("kuozhan","wwww") + } +``` diff --git a/app/src/main/java/zj/it/bhne/androidaop/MainActivity.kt b/app/src/main/java/zj/it/bhne/androidaop/MainActivity.kt index 168c30d..60f264f 100644 --- a/app/src/main/java/zj/it/bhne/androidaop/MainActivity.kt +++ b/app/src/main/java/zj/it/bhne/androidaop/MainActivity.kt @@ -7,6 +7,8 @@ import android.view.View import androidx.appcompat.app.AppCompatActivity import com.blankj.utilcode.util.ToastUtils import com.zj.singclick.SingleClick +import com.zj.singclick.click +import com.zj.singclick.setSafeListener import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() ,View.OnClickListener{ diff --git a/app/src/main/java/zj/it/bhne/androidaop/OnClick.kt b/app/src/main/java/zj/it/bhne/androidaop/OnClick.kt index 94e6b2a..927fa34 100644 --- a/app/src/main/java/zj/it/bhne/androidaop/OnClick.kt +++ b/app/src/main/java/zj/it/bhne/androidaop/OnClick.kt @@ -11,26 +11,26 @@ import android.view.View * */ -inline fun View.setSafeListener(crossinline action:()->Unit){ - var lastClick=0L - setOnClickListener { - val gap = System.currentTimeMillis() - lastClick - lastClick=System.currentTimeMillis() - if(gap<1500) return@setOnClickListener - action.invoke() - } -} - - -var _viewClickFlag = false -var _clickRunnable = Runnable { _viewClickFlag = false } -fun View.click(action: (view: View) -> Unit) { - setOnClickListener { - if (!_viewClickFlag) { - _viewClickFlag = true - action(it) - } - removeCallbacks(_clickRunnable) - postDelayed(_clickRunnable, 1000) - } -} \ No newline at end of file +//inline fun View.setSafeListener(crossinline action:()->Unit){ +// var lastClick=0L +// setOnClickListener { +// val gap = System.currentTimeMillis() - lastClick +// lastClick=System.currentTimeMillis() +// if(gap<1500) return@setOnClickListener +// action.invoke() +// } +//} +// +// +//var _viewClickFlag = false +//var _clickRunnable = Runnable { _viewClickFlag = false } +//fun View.click(action: (view: View) -> Unit) { +// setOnClickListener { +// if (!_viewClickFlag) { +// _viewClickFlag = true +// action(it) +// } +// removeCallbacks(_clickRunnable) +// postDelayed(_clickRunnable, 1000) +// } +//} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 581d850..43cf327 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.3.72' ext.kotlin_version = '1.3.10' repositories { google() diff --git a/singclick/build.gradle b/singclick/build.gradle index 779eb0b..916d708 100644 --- a/singclick/build.gradle +++ b/singclick/build.gradle @@ -1,4 +1,6 @@ apply plugin: 'com.android.library' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-android' apply plugin: 'android-aspectjx' android { compileSdkVersion 28 @@ -31,4 +33,9 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation "androidx.core:core-ktx:+" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + mavenCentral() } diff --git a/singclick/src/main/java/com/zj/singclick/OnClick.kt b/singclick/src/main/java/com/zj/singclick/OnClick.kt new file mode 100644 index 0000000..85b7aca --- /dev/null +++ b/singclick/src/main/java/com/zj/singclick/OnClick.kt @@ -0,0 +1,37 @@ +package com.zj.singclick + +import android.view.View + +/** + * 版权:渤海新能 版权所有 + * @author zhujiang + * 版本:1.5 + * 创建日期:2020/4/29 + * 描述:AndroidAOP + * + */ + + +inline fun View.setSafeListener(crossinline action:()->Unit){ + var lastClick=0L + setOnClickListener { + val gap = System.currentTimeMillis() - lastClick + lastClick=System.currentTimeMillis() + if(gap<1500) return@setOnClickListener + action.invoke() + } +} + + +var _viewClickFlag = false +var _clickRunnable = Runnable { _viewClickFlag = false } +fun View.click(action: (view: View) -> Unit) { + setOnClickListener { + if (!_viewClickFlag) { + _viewClickFlag = true + action(it) + } + removeCallbacks(_clickRunnable) + postDelayed(_clickRunnable, 1000) + } +} \ No newline at end of file