新建一個類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,奉組織之命為你提供參考答案,編程尚未成功,同志仍需努力!