廢話不說先上代碼
布局文件
<?xml version="1.0" encoding="utf-8"?>
<com.example.cc.eventdemo.MyRelativeLayout
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:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cc.eventdemo.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
android:textAllCaps="false"/>
<com.example.cc.eventdemo.MyButton
android:id="@+id/mybtn"
android:textAllCaps="false"
android:text="mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
</com.example.cc.eventdemo.MyButton>
</com.example.cc.eventdemo.MyRelativeLayout>
兩個(gè)自定義view的代碼
自定義MyButton
public class MyButton extends Button {
public MyButton(Context context) {
this(context,null);
}
public MyButton(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.d("123", "dispatchTouchEvent:MyButton: "+event.getAction());
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("123", "onTouchEvent: MyButton:"+event.getAction());
return super.onTouchEvent(event);
}
}
自定義RelativeLayout
public class MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context) {
super(context);
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d("123", "dispatchTouchEvent: MyRelativeLayout:"+ev.getAction());
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.d("123", "onInterceptTouchEvent: MyRelativeLayout:"+ev.getAction());
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("123", "onTouchEvent: MyRelativeLayout:"+event.getAction());
return super.onTouchEvent(event);
}
}
MainActivity代碼
public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener {
private MyButton mMyButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
MyRelativeLayout parent = findViewById(R.id.parent);
mMyButton = findViewById(R.id.mybtn);
button.setOnTouchListener(this);
parent.setOnTouchListener(this);
button.setOnClickListener(this);
parent.setOnClickListener(this);
mMyButton.setOnTouchListener(this);
mMyButton.setOnClickListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("123", "onTouch,View" + v + ",Action:" + event.getAction());
return false;
}
@Override
public void onClick(View v) {
Log.d("123", "onClick,View" + v);
}
}
點(diǎn)擊mybutton運(yùn)行打印結(jié)果:
結(jié)論:父容器先拿到down事件分別調(diào)用dispatchTouchEvent向下分發(fā),onInterceptTouchEvent攔截事件此時(shí)不攔截,向子view(MyButton)分發(fā)down事件,MyButton獲取down事件調(diào)用onTouch,再調(diào)onTouchEvent方法響應(yīng)down事件.當(dāng)松開手指父容器先獲取up事件,執(zhí)行順序跟down事件一樣,如果MyButton設(shè)置了點(diǎn)擊事件會調(diào)用onClick