程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 事件傳遞-為什麼onTouchEvent方法會在dispatchTouchEvent方法之前執行呢?

事件傳遞-為什麼onTouchEvent方法會在dispatchTouchEvent方法之前執行呢?

編輯:編程解疑
為什麼onTouchEvent方法會在dispatchTouchEvent方法之前執行呢?

新建一個類MyButton,該類繼承自Button
然後重寫其中的onTouchEvent和dispatchTouchEvent方法

 public class MyButton extends Button {

    private Context mContext;

    public MyButton(Context context) {
        super(context);
        this.mContext = context;
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.d("TTTT", "context:" + mContext + "MyButton|dispatchTouchEvent|return:" + super.dispatchTouchEvent(event) + "|event:DOWN");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d("TTTT", "context:" + mContext + "MyButton|dispatchTouchEvent|return:" + super.dispatchTouchEvent(event) + "|event:MOVE");
            break;
        case MotionEvent.ACTION_UP:
            Log.d("TTTT", "context:" + mContext + "MyButton|dispatchTouchEvent|return:" + super.dispatchTouchEvent(event) + "|event:UP");
            break;
        }
        Log.d("TTTT", "========================== ");
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.d("TTTT", "context:" + mContext + "MyButton|onTouchEvent|return:" + super.onTouchEvent(event) + "|event:DOWN");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d("TTTT", "context:" + mContext + "MyButton|onTouchEvent|return:" + super.onTouchEvent(event) + "|event:MOVE");
            break;
        case MotionEvent.ACTION_UP:
            Log.d("TTTT", "context:" + mContext + "MyButton|onTouchEvent|return:" + super.onTouchEvent(event) + "|event:UP");
            break;
        }
        return super.onTouchEvent(event);
    }
}

然後將這個Button加入到布局文件中,然後在Activity中給這個Button設置一個onTouchListener

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testtouchevent.MainActivity" >

    <com.example.testtouchevent.MyButton
        android:id="@+id/bt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按鈕" />

</LinearLayout>

 public class MainActivity extends Activity {

    private Button bt;

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

        bt = (Button) findViewById(R.id.bt);

        bt.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
    }
}

按下按鈕時候,log顯式

 03-31 11:51:08.493: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|onTouchEvent|return:true|event:DOWN
03-31 11:51:08.493: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|dispatchTouchEvent|return:true|event:DOWN
03-31 11:51:08.493: D/TTTT(1609): ========================== 
03-31 11:51:08.493: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|onTouchEvent|return:true|event:DOWN

松開按鈕時候,log顯式:

 03-31 11:51:10.285: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|onTouchEvent|return:true|event:UP
03-31 11:51:10.285: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|dispatchTouchEvent|return:true|event:UP
03-31 11:51:10.285: D/TTTT(1609): ========================== 
03-31 11:51:10.289: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|onTouchEvent|return:true|event:UP

可見onTouchEvent方法是在dispatchTouchEvent方法之前執行的
可是View的事件傳遞機制不是dispatchTouchEvent方法先執行用來傳遞消息的嗎?
請問這是怎麼回事兒?

最佳回答:


View 中的dispatchTouchEvent,OnInterceptTouchEvent ,OnTouchEvent方法
Activity調用dispatchTouchEvent()和onTouchEvent()方法
onTouch、onkey、dispatchTouchEvent、dispatchKeyEvent等方法被執行 “兩次” 的解決方法
----------------------同志你好,我是CSDN問答機器人小N,奉組織之命為你提供參考答案,編程尚未成功,同志仍需努力!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved