首先是MainActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private MyButton button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (MyButton) findViewById(R.id.my_btn);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
Log.i("event", "MainActivity-------onTouch MotionEvent.ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i("event", "MainActivity-------onTouch MotionEvent.ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i("event", "MainActivity-------onTouch MotionEvent.ACTION_UP");
break;
}
return false;
}
});
}
}
自定义控件
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;
/**
* Created by Administrator on 2016/3/25.
*/
public class MyButton extends Button{
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
Log.i("event", "MyButton---------------onTouchEvent ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i("event", "MyButton---------------onTouchEvent ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i("event", "MyButton---------------onTouchEvent ACTION_UP");
break;
}
return super.onTouchEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
Log.i("event", "MyButton---------------dispatchTouchEvent ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i("event", "MyButton---------------dispatchTouchEvent ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i("event", "MyButton---------------dispatchTouchEvent ACTION_UP");
break;
}
return super.dispatchTouchEvent(event);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="himici.androidevent.MainActivity">
<himici.androidevent.MyButton
android:id="@+id/my_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="test click"/>
</LinearLayout>
当点击按钮之后,事件触发顺序
04-18 16:42:50.273 27820-27820/? I/event: MyButton---------------dispatchTouchEvent ACTION_DOWN
04-18 16:42:50.274 27820-27820/? I/event: MainActivity-------onTouch MotionEvent.ACTION_DOWN
04-18 16:42:50.274 27820-27820/? I/event: MyButton---------------onTouchEvent ACTION_DOWN
04-18 16:42:50.295 27820-27820/? I/event: MyButton---------------dispatchTouchEvent ACTION_MOVE
04-18 16:42:50.295 27820-27820/? I/event: MainActivity-------onTouch MotionEvent.ACTION_MOVE
04-18 16:42:50.295 27820-27820/? I/event: MyButton---------------onTouchEvent ACTION_MOVE
04-18 16:42:50.313 27820-27820/? I/event: MyButton---------------dispatchTouchEvent ACTION_MOVE
04-18 16:42:50.314 27820-27820/? I/event: MainActivity-------onTouch MotionEvent.ACTION_MOVE
04-18 16:42:50.314 27820-27820/? I/event: MyButton---------------onTouchEvent ACTION_MOVE
04-18 16:42:50.329 27820-27820/? I/event: MyButton---------------dispatchTouchEvent ACTION_MOVE
04-18 16:42:50.329 27820-27820/? I/event: MainActivity-------onTouch MotionEvent.ACTION_MOVE
04-18 16:42:50.329 27820-27820/? I/event: MyButton---------------onTouchEvent ACTION_MOVE
04-18 16:42:50.330 27820-27820/? I/event: MyButton---------------dispatchTouchEvent ACTION_UP
04-18 16:42:50.330 27820-27820/? I/event: MainActivity-------onTouch MotionEvent.ACTION_UP
04-18 16:42:50.330 27820-27820/? I/event: MyButton---------------onTouchEvent ACTION_UP