Skip to content

Commit

Permalink
加上了评论区大家提到的扩展函数
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujiang521 committed Apr 29, 2020
1 parent a1316b8 commit 34ff307
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/zj/it/bhne/androidaop/Main2Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ public void btnAop(View view) {
Log.e(TAG, "btnAop");
}

public void btnKotlin(View view) {
startActivity(new Intent(this,MainActivity.class));
}
}
15 changes: 14 additions & 1 deletion app/src/main/java/zj/it/bhne/androidaop/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ class MainActivity : AppCompatActivity() ,View.OnClickListener{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnClick.setOnClickListener(this)
initView()
}

private fun initView() {
btnKuoZhan.setSafeListener {
ToastUtils.showShort("222")
Log.e("kuozhan","wwww")
}

btnKuoZhanClick.click {
ToastUtils.showShort("ss")
Log.e("kuozhan","呵呵呵")
}
}


Expand All @@ -25,7 +38,7 @@ class MainActivity : AppCompatActivity() ,View.OnClickListener{
when(v.id){
R.id.btnClick->{
ToastUtils.showShort("哈哈哈")
Log.e("刘佩希","wwww")
Log.e("ee","wwww")
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/zj/it/bhne/androidaop/OnClick.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package zj.it.bhne.androidaop

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)
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<Button
Expand All @@ -10,4 +11,16 @@
android:layout_height="wrap_content"
android:text="重复点击"/>

<Button
android:id="@+id/btnKuoZhan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="扩展新方法实现"/>

<Button
android:id="@+id/btnKuoZhanClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="扩展OnClick方法实现"/>

</LinearLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
android:layout_gravity="center"
android:onClick="btnDialog"/>

<Button
android:id="@+id/btnKotlin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kotlin"
android:layout_gravity="center"
android:onClick="btnKotlin"/>

<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class SingleClickAspect {

/**
* 定义切点,标记切点为所有被@SingleClick注解的方法
* 注意:这里me.baron.test.annotation.SingleClick需要替换成
* 你自己项目中SingleClick这个类的全路径哦
* com.zj.singclick.SingleClick需要替换成
* 自己项目中SingleClick这个类的全路径哦
*/
@Pointcut("execution(@com.zj.singclick.SingleClick * *(..))")
public void methodAnnotated() {}
Expand Down
1 change: 1 addition & 0 deletions test/src/main/java/com/zj/test/TestActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ class TestActivity : AppCompatActivity(), View.OnClickListener {
override fun onClick(v: View) {
Toast.makeText(this,"测试",Toast.LENGTH_SHORT).show()
Log.e("测试时","cee")
//startActivity<TestActivity>(this){}
}
}
19 changes: 19 additions & 0 deletions test/src/main/java/com/zj/test/reified.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.zj.test

import android.content.Context
import android.content.Intent

/**
* 版权:渤海新能 版权所有
* @author zhujiang
* 版本:1.5
* 创建日期:2020/4/27
* 描述:AndroidAOP
*
*/

inline fun <reified T> startActivity(context: Context,block: Intent.() -> Unit){
val intent = Intent(context, T::class.java)
intent.block()
context.startActivity(intent)
}

0 comments on commit 34ff307

Please sign in to comment.