在看自定义的过程中,onInterceptTouchEvent和onTouchEvent方法经常用到,但是对其的区别却不甚了解。看了几篇博客,对这两个方法有了大概的理解,现以此博客来加深印象。
onInterceptTouchEvent继承ViewGroup,表示不拦截touchEvent事件;它的默认值为false。
onTouchEvent继承view,表示触摸事件。viewGroup调用onTouchEvent时,它的默认值为false;View调用onTouchEvent时,它的默认值为true。
下面通过例子来具体看一下:
自定义MyButton类继承View
public class MyButton extends Button {
public MyButton(Context context) {
super(context);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("MyButton-->onTouchEvent", "ACTION_DOWN");
break;
case MotionEvent.ACTION_UP:
Log.e("MyButton-->onTouchEvent", "ACTION_UP");
break;
case MotionEvent.ACTION_MOVE:
Log.e("MyButton-->onTouchEvent", "ACTION_MOVE");
break;
default:
break;
}
Log.e("MyButton-->onTouchEvent-->default", "Default:" + super.onTouchEvent(event));
return super.onTouchEvent(event);
}
}
自定义MyLinearLayout类继承ViewGroup:
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("MyLinearLayout-->onInterceptTouchEvent", "ACTION_DOWN");
break;
case MotionEvent.ACTION_UP:
Log.e("MyLinearLayout-->onInterceptTouchEvent", "ACTION_UP");
break;
case MotionEvent.ACTION_MOVE:
Log.e("MyLinearLayout-->onInterceptTouchEvent", "ACTION_MOVE");
break;
default:
break;
}
Log.e("MyLinearLayout-->onInterceptTouchEvent-->default", "Default:" + super.onInterceptTouchEvent(ev));
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("MyLinearLayout-->onTouchEvent", "ACTION_DOWN");
break;
case MotionEvent.ACTION_UP:
Log.e("MyLinearLayout-->onTouchEvent", "ACTION_UP");
break;
case MotionEvent.ACTION_MOVE:
Log.e("MyLinearLayout-->onTouchEvent", "ACTION_MOVE");
break;
default:
break;
}
Log.e("MyLinearLayout-->onTouchEvent-->default", "Default:" + super.onTouchEvent(event));
return super.onTouchEvent(event);
}
}
在XML中,将MyButton嵌套到MyLinearLayout中:
<com.tbwy.comparemethod.view.MyLinearLayout 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" >
<com.tbwy.comparemethod.view.MyButton
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</com.tbwy.comparemethod.view.MyLinearLayout>
在MainActivity中的代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
从上面的输出值可以看出,onInterceptTouchEvent的默认值为false,MyButton中onTouchEvent的返回值为true;而MyLinearLayout中的onTouchEvent并没有执行。但是都触发了ACION_DOWN,ACTION_UP
如果按下时,多停留几秒,输出值:
这时触发了ACTION_MOVE
2,将onInterceptTouchEvent的返回值设为true,其它不变
从上面输出值可以看出,onInterceptTouchEvent的值为true时,拦截了触摸事件,MyButton中onTouchEvent方法并没有执行;而执行了MyLinearLayout中的onTouchEvent方法,且其默认值为false。onInterceptTouchEvent和MyLinearLayout中的onTouchEvent方法,只触发了ACTION_DOWN
3,将onInterceptTouchEvent的返回值设为false,MyButton中onTouchEvent返回值设为false,其它不变
从上面的值可以看出,3个方法都调用了,但是都只是触发了ACTION_DOWN