1209-View:事件分發(fā)機制二

先看示例代碼

<?xml version="1.0" encoding="utf-8"?>
<com.fy.example.touchevent.view.TouchRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.fy.example.touchevent.view.TouchTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, my TouchEventDemo!" />
</com.fy.example.touchevent.view.TouchRelativeLayout>

兩個自定義控件,重寫了相關(guān)方法,便于考察事件分發(fā)

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d("MainActivity", "dispatchTouchEvent()");
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("MainActivity", "onTouchEvent()");
        return super.onTouchEvent(event);
    }
}
public class TouchRelativeLayout extends RelativeLayout {

    public TouchRelativeLayout(Context context) {
        super(context);
    }
    public TouchRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public TouchRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d("TouchRelativeLayout", "dispatchTouchEvent()");
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d("TouchRelativeLayout", "onInterceptTouchEvent()");
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("TouchRelativeLayout", "onTouchEvent()");
        return super.onTouchEvent(event);
    }
}
public class TouchTextView extends TextView {

    public TouchTextView(Context context) {
        super(context);
    }
    public TouchTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public TouchTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d("TouchTextView", "dispatchTouchEvent()");
        return super.dispatchTouchEvent(ev);
    }

    @Override
        public boolean onTouchEvent(MotionEvent event) {
        Log.d("TouchTextView", "onTouchEvent()");
        return false;
    }
}

點擊一下界面中的文本,日志打印如下

12-09 03:13:22.153 1673-1673/com.fy.example.touchevent D/MainActivity: dispatchTouchEvent()
12-09 03:13:22.153 1673-1673/com.fy.example.touchevent D/TouchRelativeLayout: dispatchTouchEvent()
12-09 03:13:22.153 1673-1673/com.fy.example.touchevent D/TouchRelativeLayout: onInterceptTouchEvent()
12-09 03:13:22.153 1673-1673/com.fy.example.touchevent D/TouchTextView: dispatchTouchEvent()
12-09 03:13:22.153 1673-1673/com.fy.example.touchevent D/TouchTextView: onTouchEvent()
12-09 03:13:22.153 1673-1673/com.fy.example.touchevent D/TouchRelativeLayout: onTouchEvent()
12-09 03:13:22.153 1673-1673/com.fy.example.touchevent D/MainActivity: onTouchEvent()
12-09 03:13:22.235 1673-1673/com.fy.example.touchevent D/MainActivity: dispatchTouchEvent()
12-09 03:13:22.235 1673-1673/com.fy.example.touchevent D/MainActivity: onTouchEvent()
12-09 03:13:22.253 1673-1673/com.fy.example.touchevent D/MainActivity: dispatchTouchEvent()
12-09 03:13:22.253 1673-1673/com.fy.example.touchevent D/MainActivity: onTouchEvent()

可見,事件傳遞過程是由外向內(nèi),即事件總是先傳遞給父元素,然后由父元素分發(fā)給子元素。

而由于TextView是不可點擊的,它的onTouchEvent默認返回false,這樣事件被層層拋上來,最終交給Activity處理。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容