以上所有的答案是正确的,但结果不同的是,如果该视图clickable
或不clickable
例如,我有一个LinearLayout
包含1 Button
和1 TextView
这样的
<LinearLayout
android:id="@+id/linearlayout_root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0aa"
android:orientation="vertical">
<Button
android:id="@+id/button_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="40dp"
android:text="Button Click"
android:textSize="20sp" />
<TextView
android:id="@+id/textview_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="40dp"
android:text="TextView Click"
android:textSize="20sp"
android:background="#e4e4e4"
/>
</LinearLayout>
在活动中,我有类似的代码
class MainActivity : AppCompatActivity() {
val TAG = "TAG"
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<LinearLayout>(R.id.linearlayout_root).setOnTouchListener { v, event ->
Log.i(TAG, "LinearLayout onTouch event " + getDisplayAction(event.action))
false
}
findViewById<Button>(R.id.button_click).setOnTouchListener { v, event ->
Log.i(TAG, "Button onTouch event " + getDisplayAction(event.action))
false
}
findViewById<TextView>(R.id.textview_click).setOnTouchListener { v, event ->
Log.i(TAG, "TextView onTouch event " + getDisplayAction(event.action))
false
}
}
private fun getDisplayAction(action: Int): String {
return when (action) {
MotionEvent.ACTION_DOWN -> "DOWN"
MotionEvent.ACTION_MOVE -> "MOVE"
MotionEvent.ACTION_UP -> "UP"
MotionEvent.ACTION_CANCEL -> "CANCEL"
MotionEvent.ACTION_OUTSIDE -> "OUTSIDE"
else -> "UNKNOWN"
}
}
}
案例1 Linear onTouch return **FALSE**
,Button onTouch return **FALSE**
,TextView onTouch return **FALSE**
点击按钮
I/TAG: Button onTouch eventDOWN
I/TAG: Button onTouch eventMOVE
I/TAG: Button onTouch eventUP
点击TextView
TAG: TextView onTouch eventDOWN
TAG: LinearLayout onTouch eventDOWN
点击LinearLayout
TAG: LinearLayout onTouch eventDOWN
案例2 Linear onTouch return **FALSE**
,Button onTouch return **TRUE**
,TextView onTouch return **TRUE**
点击按钮
Similar to case 1
点击TextView
TAG: TextView onTouch event DOWN
TAG: TextView onTouch event MOVE
TAG: TextView onTouch event UP
点击LinearLayout
Similar to case 1
案例3 Linear onTouch return **TRUE**
,Button onTouch return **FALSE**
,TextView onTouch return **FALSE**
点击按钮
Similar to case 1
点击TextView
TAG: TextView onTouch event DOWN
TAG: LinearLayout onTouch event DOWN
TAG: LinearLayout onTouch event MOVE
TAG: LinearLayout onTouch event UP
点击LinearLayout
TAG: LinearLayout onTouch event DOWN
TAG: LinearLayout onTouch event MOVE
TAG: LinearLayout onTouch event UP
注意
- 默认
TextView
值为not clickable
,如果我们设置android:clickable="true"
为xml 或设置时,它将变为可点击textView.setOnClickListener(...)
- 调试时,
event MOVE
可以调用的日志超过我的日志(取决于您的点击方式)
摘要
onTouch
return true
或view是clickable
,View将接收全部 onTouchEvent
onTouch
返回false
并且视图不是clickable
,视图将不会收到NEXT onTouchEvent(它的父级可能会收到)
希望它能帮助
演示