Skip to content

Commit bfd4694

Browse files
author
Faqiang Lv
committed
View OnClickListener 获取、移除。
1 parent 76b6ddc commit bfd4694

File tree

5 files changed

+183
-6
lines changed

5 files changed

+183
-6
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
android:supportsRtl="true"
2020
android:theme="@style/AppTheme">
2121

22-
<activity android:name=".coroutines.thread_toggle.CoroutineToggleActivity">
22+
<activity android:name=".view.click.ViewClickActivity">
2323
<intent-filter>
2424
<action android:name="android.intent.action.MAIN" />
2525

app/src/main/java/com/lvfq/code/coroutines/thread_toggle/CoroutineToggleActivity.kt

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import android.support.v7.app.AppCompatActivity
55
import android.view.View
66
import com.lvfq.code.R
77
import com.lvfq.library.utils.LvLog
8+
import kotlinx.android.synthetic.main.activity_coroutine_toggle.*
9+
import kotlinx.coroutines.experimental.android.UI
10+
import kotlinx.coroutines.experimental.delay
11+
import kotlinx.coroutines.experimental.launch
812

913
/**
1014
* CoroutineToggleActivity
@@ -24,12 +28,27 @@ class CoroutineToggleActivity : AppCompatActivity() {
2428
}
2529

2630
fun button1(v: View) {
27-
CoroutineJava.async(object : Task() {
28-
override fun run() {
29-
Thread.sleep(1000)
30-
LvLog.i("lfq", "task==========${Thread.currentThread().name}")
31+
launch(AndroidCommonPool) {
32+
delay(4000)
33+
LvLog.i("lfq", "launch==========${Thread.currentThread().name}")
34+
launch(UI) {
35+
val result = kotlinx.coroutines.experimental.async {
36+
delay(3000)
37+
"resultString"
38+
}
39+
LvLog.i("lfq", "before ====${Thread.currentThread().name}")
40+
tv_content.text = result.await()
41+
LvLog.i("lfq", "after text:${tv_content.text}==== ${Thread.currentThread().name}")
42+
LvLog.i("lfq", "this Activity is Finished $isFinishing")
3143
}
32-
})
44+
}
45+
46+
// CoroutineJava.async(object : Task() {
47+
// override fun run() {
48+
// Thread.sleep(1000)
49+
// LvLog.i("lfq", "task==========${Thread.currentThread().name}")
50+
// }
51+
// })
3352
}
3453

3554
fun button2(v: View) {
@@ -39,4 +58,9 @@ class CoroutineToggleActivity : AppCompatActivity() {
3958
}
4059
})
4160
}
61+
62+
override fun onDestroy() {
63+
super.onDestroy()
64+
LvLog.i("lfq", " OnDestroy")
65+
}
4266
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.lvfq.code.view.click
2+
3+
import android.os.Bundle
4+
import android.support.v7.app.AppCompatActivity
5+
import android.util.Log
6+
import android.view.View
7+
import com.lvfq.code.R
8+
import kotlinx.android.synthetic.main.activity_view_click.*
9+
import java.lang.reflect.Method
10+
11+
/**
12+
* ViewClickActivity
13+
* @author FaQiang on 2018/9/1 下午2:39
14+
* @Github: <a href="https://github.com/lvfaqiang"/>
15+
* @Blog: <a href="http://blog.csdn.net/lv_fq"/>
16+
* @desc :
17+
*
18+
*/
19+
class ViewClickActivity : AppCompatActivity() {
20+
21+
override fun onCreate(savedInstanceState: Bundle?) {
22+
super.onCreate(savedInstanceState)
23+
setContentView(R.layout.activity_view_click)
24+
25+
26+
vc_tv_click.setOnClickListener {
27+
Log.i("TAG", "ViewClick")
28+
}
29+
val click = getOnClickListener(vc_tv_click)
30+
31+
vc_btn_cancel.setOnClickListener {
32+
removeOnClickListener(vc_tv_click)
33+
}
34+
vc_btn_reset.setOnClickListener {
35+
vc_tv_click.setOnClickListener(click)
36+
}
37+
}
38+
39+
fun getOnClickListener(view: View): View.OnClickListener? {
40+
val info = getListenerInfo(view)
41+
info?.let {
42+
val m = getFieldValue(it, "mOnClickListener") as View.OnClickListener?
43+
return m
44+
}
45+
return null
46+
}
47+
48+
fun removeOnClickListener(view: View) {
49+
val info = getListenerInfo(view)
50+
info?.let {
51+
setFieldValue(it, "mOnClickListener", null)
52+
}
53+
}
54+
55+
56+
fun getListenerInfo(view: View): Any? {
57+
val method = getDeclaredMethod(view, "getListenerInfo")
58+
method?.isAccessible = true
59+
val info = method?.invoke(view)
60+
return info
61+
}
62+
63+
64+
fun setFieldValue(obj: Any?, fieldName: String, value: Any?) {
65+
if (obj == null || fieldName.isEmpty()) {
66+
return
67+
}
68+
val clazz: Class<*> = obj.javaClass
69+
while (clazz != Any::class.java) {
70+
try {
71+
val field = clazz.getDeclaredField(fieldName)
72+
field.isAccessible = true
73+
field.set(obj, value)
74+
return
75+
} catch (e: Exception) {
76+
}
77+
}
78+
}
79+
80+
fun getFieldValue(obj: Any?, fieldName: String): Any? {
81+
if (obj == null || fieldName.isEmpty()) {
82+
return null
83+
}
84+
val clazz: Class<*> = obj.javaClass
85+
if (clazz != Any::class.java) {
86+
try {
87+
val field = clazz.getDeclaredField(fieldName)
88+
field.isAccessible = true
89+
return field.get(obj)
90+
} catch (e: Exception) {
91+
}
92+
}
93+
return null
94+
}
95+
96+
/**
97+
* https://blog.csdn.net/zmx729618/article/details/51320688
98+
*
99+
* 获取当前类型的 某个方法
100+
*/
101+
fun getDeclaredMethod(obj: Any, methodName: String, vararg parameterTypes: Class<*>): Method? {
102+
var method: Method? = null
103+
var clazz: Class<*> = obj.javaClass
104+
while (clazz != Any::class.java) {
105+
try {
106+
method = clazz.getDeclaredMethod(methodName, *parameterTypes)
107+
// method?.isAccessible = true
108+
return method
109+
} catch (e: Exception) {
110+
//这里甚么都不要做!并且这里的异常必须这样写,不能抛出去。
111+
//如果这里的异常打印或者往外抛,则就不会执行clazz = clazz.getSuperclass(),最后就不会进入到父类中了
112+
}
113+
clazz = clazz.superclass
114+
}
115+
return null
116+
}
117+
118+
}

app/src/main/res/layout/activity_coroutine_toggle.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@
1818
android:layout_height="wrap_content"
1919
android:onClick="button2"
2020
android:text="UI" />
21+
22+
<TextView
23+
android:id="@+id/tv_content"
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content"
26+
android:textColor="@color/color_b6"
27+
android:textSize="@dimen/sp_14" />
2128
</LinearLayout>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<Button
8+
android:id="@+id/vc_tv_click"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:gravity="center"
12+
android:text="点击事件" />
13+
14+
15+
<Button
16+
android:id="@+id/vc_btn_cancel"
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"
19+
android:text="取消点击事件" />
20+
21+
<Button
22+
android:id="@+id/vc_btn_reset"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:text="重新添加点击事件" />
26+
27+
28+
</LinearLayout>

0 commit comments

Comments
 (0)