What is meaning of boolean value returned from an event-handling method in Android
In android, most event listener methods return a boolean value. What is that true/false value mean ? what will it result in to the subsequence events ?
class MyTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
logView.showEvent(event);
return true;
}
}
Regarding to the above example, if return true in onTouch method,I found every touch event(DOWN,UP,MOVE,etc) has been captured according to my logView. On the contrary,if return false, onely the DOWN event been captured. So it's seemd that return false will prevent the event to propagate. Am I correct ?开发者_运维知识库
Furthermore, in a OnGestureListener, many methods have to return a boolean value too. Do they have the same meaning ?
If you return true
from an ACTION_DOWN
event you are interested in the rest of the events in that gesture. A "gesture" in this case means all events until the final ACTION_UP
or ACTION_CANCEL
. Returning false
from an ACTION_DOWN
means you do not want the event and other views will have the opportunity to handle it. If you have overlapping views this can be a sibling view. If not it will bubble up to the parent.
From the documentation : http://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch(android.view.View, android.view.MotionEvent)
"True if the listener has consumed the event, false otherwise."
If you return true, the event is processed. If false, it will go to the next layer down.
The boolean value determines whether the event is consumed or not.
Yes you're correct. If you return false, the next listener handles the event. If it returns true, the event is consumed by your listener and not sent to the next method.
All above answer is correct but the result is different is if the View is clickable
or not clickable
Example, I have a LinearLayout
contains 1 Button
and 1 TextView
like this
<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>
In Activity, I have code like
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"
}
}
}
Case 1 Linear onTouch return **FALSE**
, Button onTouch return **FALSE**
, TextView onTouch return **FALSE**
Click on Button
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
Case 2 Linear onTouch return **FALSE**
, Button onTouch return **TRUE**
, TextView onTouch return **TRUE**
Click on Button
Similar to case 1
Click on TextView
TAG: TextView onTouch event DOWN
TAG: TextView onTouch event MOVE
TAG: TextView onTouch event UP
Click on LinearLayout
Similar to case 1
Case 3 Linear onTouch return **TRUE**
, Button onTouch return **FALSE**
, TextView onTouch return **FALSE**
Click on Button
Similar to case 1
Click on TextView
TAG: TextView onTouch event DOWN
TAG: LinearLayout onTouch event DOWN
TAG: LinearLayout onTouch event MOVE
TAG: LinearLayout onTouch event UP
Click on LinearLayout
TAG: LinearLayout onTouch event DOWN
TAG: LinearLayout onTouch event MOVE
TAG: LinearLayout onTouch event UP
Note
- Default of
TextView
isnot clickable
, it will become clickable if we setandroid:clickable="true"
in xml OR when we settextView.setOnClickListener(...)
- When you debug,
event MOVE
can call more than my log (it base on how you tap)
Summary
onTouch
returntrue
or view isclickable
, View will receive allonTouchEvent
onTouch
returnfalse
and view is notclickable
, view will not receive NEXT onTouchEvent (it's parent may receive it)
Hope it help
DEMO
I lost nearly one day in troubleshooting, still i found out, that my onTouch function is called 2 times when using true and 1 times when using false.
From Android-document:
Note: Android will call event handlers first and then the appropriate default handlers from the class definition second. As such, returning true from these event listeners will stop the propagation of the event to other event listeners and will also block the callback to the default event handler in the View. So be certain that you want to terminate the event when you return true.
精彩评论