위의 모든 대답은 정확하지만 결과는 View가 clickable있는지 여부입니다.clickable
예 , 나는 이것 과 같은 LinearLayout1 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반환 true또는보기는 clickable
,보기는 모두 수신 합니다. onTouchEvent
onTouchreturn false및 view is not clickable, view will not receive NEXT onTouchEvent (it 's parent may receive it)
데모가 도움이
되기를 바랍니다.