IT story

Android에서 이벤트 처리 방법에서 반환되는 부울 값의 의미

hot-time 2020. 8. 6. 07:50
반응형

Android에서 이벤트 처리 방법에서 반환되는 부울 값의 의미


안드로이드에서 대부분의 이벤트 리스너 메소드는 부울 값을 반환합니다. 그 참 / 거짓 값은 무엇을 의미합니까? 하위 시퀀스 이벤트의 결과는 무엇입니까?

class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        logView.showEvent(event);
        return true;
    }
}

위의 예와 관련하여 onTouch 메서드 에서 true를 반환하면 모든 터치 이벤트 (DOWN, UP, MOVE 등)가 내 logView 에 따라 캡처 된 것을 알았습니다 . 반대로 false를 반환하면 DOWN 이벤트 중 하나가 캡처 된 것입니다. 따라서 false를 반환하면 이벤트가 전파되지 않습니다. 나 맞아 ?

또한 OnGestureListener 에서 많은 메소드가 부울 값도 리턴해야합니다. 그들은 같은 의미를 가지고 있습니까?


이벤트 true에서 돌아 오면 ACTION_DOWN해당 제스처의 나머지 이벤트에 관심이 있습니다. 이 경우 "제스처"는 최종 ACTION_UP또는 까지 모든 이벤트를 의미합니다 ACTION_CANCEL. 반환 false에서 ACTION_DOWN의미하는 것은 당신이 이벤트를 원하지 않는 다른 뷰를 처리 할 수있는 기회를 갖게됩니다. 겹치는보기가 있으면 형제보기 일 수 있습니다. 그렇지 않으면 부모에게 버블 링됩니다.


설명서에서 : http://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch(android.view.View , android.view.MotionEvent)

"리스너가 이벤트를 소비 한 경우 참, 그렇지 않으면 거짓."

true를 리턴하면 이벤트가 처리됩니다. False면 다음 레이어로 내려갑니다.


부울 값은 이벤트 사용 여부를 결정합니다.

네 맞습니다. false를 리턴하면 다음 리스너가 이벤트를 처리합니다. true를 반환하면 리스너가 이벤트를 사용하고 다음 메소드로 보내지 않습니다.


위의 모든 대답은 정확하지만 결과가 다릅니다.보기가 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>

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 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로 설정하면 OR 을 설정할 때 클릭 할 수있게됩니다.textView.setOnClickListener(...)
  • When you debug, event MOVE can call more than my log (it base on how you tap)

Summary

  • onTouch return true or view is clickable , View will receive all onTouchEvent
  • onTouch return false and view is not clickable, 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.

참고URL : https://stackoverflow.com/questions/3756383/what-is-meaning-of-boolean-value-returned-from-an-event-handling-method-in-andro

반응형