以上所有答案都是正确的,但结果的不同在于View是否可点击clickable
例如,我有一个包含1个Button
和1个TextView
的LinearLayout
,如下所示
<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>
在Activity中,我有以下代码:
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:线性触摸返回**FALSE**
,按钮触摸返回**FALSE**
,文本视图触摸返回**FALSE**
点击按钮
I/TAG: Button onTouch eventDOWN
I/TAG: Button onTouch eventMOVE
I/TAG: Button onTouch eventUP
Click on TextView
TAG: TextView onTouch eventDOWN
TAG: LinearLayout onTouch eventDOWN
Click on LinearLayout
TAG: LinearLayout onTouch eventDOWN
案例2: Linear onTouch 返回 **FALSE**
,Button onTouch 返回 **TRUE**
,TextView onTouch 返回 **TRUE**
点击按钮
Similar to case 1
Click on TextView
TAG: TextView onTouch event DOWN
TAG: TextView onTouch event MOVE
TAG: TextView onTouch event UP
点击LinearLayout
Similar to case 1
案例3 Linear onTouch 返回 **TRUE**
,Button onTouch 返回 **FALSE**
,TextView onTouch 返回 **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
的默认值为不可点击
,如果我们在xml中设置android:clickable="true"
或者使用textView.setOnClickListener(...)
来设置,它将变得可点击。
- 当您调试时,
MOVE
事件可能会调用多次(这取决于您的点击方式)。
总结
- 如果
onTouch
返回true
或视图是clickable
,则视图将接收所有onTouchEvent
。
- 如果
onTouch
返回false
且视图不可clickable
,则视图将不会接收下一个onTouchEvent
(其父级可能会接收它)。
希望这有所帮助
演示