Android(安卓)中 OnTouch事件的研究
在Android中当一个ViewGroup 中包含一个组件,当点击这个组件同时监听其onTouch事件,那么到底是父组件响应还是子组件响应呢?傻蛋在网上找到了一篇相关的帖子,照着例子测试了一番,发现网上的那篇有点问题,先把测试结果总结如下.
在触发OnTouch事件的时候Android的GroupView会调用如下三个函数:
public boolean dispatchTouchEvent(MotionEvent ev) 用于事件的分发
public boolean onInterceptTouchEvent(MotionEvent ev) 用于事件的拦截
public boolean onTouchEvent(MotionEvent ev) 处理事件
当然我们可以在容器类中如继承自LinearLayout的类中重写这三个方法。而继承View类的子类只能重写dispatch和onTouchEvent两个方法。当点击后这三个方法相继执行。
自己写了一个TextView子类MyTextView和LinearLayout子类MyLinearLayout,TextView包含在LinearLayout中。
当点击MyTextView时,程序会先进入到LinearLayout的dispatchTouchEvent中,这个类必须调用super.dispatchTouchEvent(ev); 否在后面的两个方法无法触发,所以傻蛋发现这个方法根本没有必要重写,因为框架是在super.dispatchTouchEvent(ev);中来调用onInterceptTouchEvent和onTouchEvent方法的,所以手动的设置dispatchTouchEvent的返回值是无效的,除非你不想让框架触发这两个方法。
当执行完dispathTouchEvent后会执行onInterception方法,如果返回为true,这表示MyLinearLayout把这个Touch事件拦截了,就会执行自己的Ontouch方法。如果为false则表示不拦截,此事件会分发到把事件传递给它的子控件MyTextView中。
当事件传递到MyTextView后,会执行dispatchTouchEvent,然后会执行onTouchEvent。挡在MyTextView中的onTouchEvent返回为false的话,当执行完onTouchEvent中的事件后,事件会再分发给MyLinearLaytout,执行LinearLayout的onTouchEvent。
主要代码如下
?[Copy to clipboard] Download zuiniuwang.java?[Copy to clipboard] Download zuiniuwang.xml
- /**
- *MyLinearLayout.java
- *com.androidtest.touch.test
- *
- *Function:TODO
- *
- *verdateauthor
- *──────────────────────────────────
- *2011-5-24Leon
- *
- *Copyright(c)2011,TNTAllRightsReserved.
- */
- packagecom.androidtest.touch.test;
- importandroid.content.Context;
- importandroid.util.AttributeSet;
- importandroid.util.Log;
- importandroid.view.MotionEvent;
- importandroid.widget.LinearLayout;
- /**
- *ClassName:MyLinearLayoutFunction:TODOADDFUNCTIONReason:TODOADDREASON
- *
- *@authorLeon
- *@version
- *@sinceVer1.1
- *@Date2011-5-24
- */
- publicclassMyLinearLayoutextendsLinearLayout{
- privatefinalstaticStringTAG=MyLinearLayout.class.getSimpleName();
- publicMyLinearLayout(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- //TODOAuto-generatedconstructorstub
- Log.v(TAG,TAG);
- }
- @Override
- publicbooleandispatchTouchEvent(MotionEventev){
- intaction=ev.getAction();
- switch(action){
- caseMotionEvent.ACTION_DOWN:
- Log.d(TAG,"dispatchTouchEventaction:ACTION_DOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d(TAG,"dispatchTouchEventaction:ACTION_MOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d(TAG,"dispatchTouchEventaction:ACTION_UP");
- break;
- caseMotionEvent.ACTION_CANCEL:
- Log.d(TAG,"dispatchTouchEventaction:ACTION_CANCEL");
- break;
- }
- //Log.v(TAG,"dispatchTouchEvent"+super.dispatchTouchEvent(ev));
- super.dispatchTouchEvent(ev);
- Log.v(TAG,"dispatchTouchEvent"+"test.................");
- returntrue;
- }
- @Override
- publicbooleanonInterceptTouchEvent(MotionEventev){
- intaction=ev.getAction();
- switch(action){
- caseMotionEvent.ACTION_DOWN:
- Log.d(TAG,"onInterceptTouchEventaction:ACTION_DOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d(TAG,"onInterceptTouchEventaction:ACTION_MOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d(TAG,"onInterceptTouchEventaction:ACTION_UP");
- break;
- caseMotionEvent.ACTION_CANCEL:
- Log.d(TAG,"onInterceptTouchEventaction:ACTION_CANCEL");
- break;
- }
- returnfalse;
- }
- @Override
- publicbooleanonTouchEvent(MotionEventev){
- intaction=ev.getAction();
- switch(action){
- caseMotionEvent.ACTION_DOWN:
- Log.d(TAG,"---onTouchEventaction:ACTION_DOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d(TAG,"---onTouchEventaction:ACTION_MOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d(TAG,"---onTouchEventaction:ACTION_UP");
- break;
- caseMotionEvent.ACTION_CANCEL:
- Log.d(TAG,"---onTouchEventaction:ACTION_CANCEL");
- break;
- }
- returntrue;
- }
- }
- /**
- *MyTestView.java
- *com.androidtest.touch.test
- *
- *Function:TODO
- *
- *verdateauthor
- *──────────────────────────────────
- *2011-5-24Leon
- *
- *Copyright(c)2011,TNTAllRightsReserved.
- */
- packagecom.androidtest.touch.test;
- importandroid.content.Context;
- importandroid.util.AttributeSet;
- importandroid.util.Log;
- importandroid.view.MotionEvent;
- importandroid.widget.TextView;
- /**
- *ClassName:MyTestViewFunction:TODOADDFUNCTIONReason:TODOADDREASON
- *
- *@authorLeon
- *@version
- *@sinceVer1.1
- *@Date2011-5-24
- */
- publicclassMyTestViewextendsTextView{
- publicstaticfinalStringTAG=MyTestView.class.getSimpleName();
- publicMyTestView(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- //TODOAuto-generatedconstructorstub
- Log.v(TAG,TAG);
- }
- @Override
- publicbooleandispatchTouchEvent(MotionEventev){
- intaction=ev.getAction();
- switch(action){
- caseMotionEvent.ACTION_DOWN:
- Log.d(TAG,"dispatchTouchEventaction:ACTION_DOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d(TAG,"dispatchTouchEventaction:ACTION_MOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d(TAG,"dispatchTouchEventaction:ACTION_UP");
- break;
- caseMotionEvent.ACTION_CANCEL:
- Log.d(TAG,"onTouchEventaction:ACTION_CANCEL");
- break;
- }
- returnsuper.dispatchTouchEvent(ev);
- }
- @Override
- publicbooleanonTouchEvent(MotionEventev){
- intaction=ev.getAction();
- switch(action){
- caseMotionEvent.ACTION_DOWN:
- Log.d(TAG,"---onTouchEventaction:ACTION_DOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d(TAG,"---onTouchEventaction:ACTION_MOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d(TAG,"---onTouchEventaction:ACTION_UP");
- break;
- caseMotionEvent.ACTION_CANCEL:
- Log.d(TAG,"---onTouchEventaction:ACTION_CANCEL");
- break;
- }
- returnfalse;
- }
- }
- /**
- *TestTouchEvent.java
- *com.androidtest.touch.test
- *
- *Function:TODO
- *
- *verdateauthor
- *──────────────────────────────────
- *2011-5-24Leon
- *
- *Copyright(c)2011,TNTAllRightsReserved.
- */
- packagecom.androidtest.touch.test;
- importcom.androidtest.R;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- /**
- *ClassName:TestTouchEvent
- *Function:TODOADDFUNCTION
- *Reason:TODOADDREASON
- *
- *@authorLeon
- *@version
- *@sinceVer1.1
- *@Date2011-5-24
- */
- publicclassTestTouchEventextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.test_touch_event);
- }
- }
- <?xmlversion="1.0"encoding="utf-8"?>
- <com.androidtest.touch.test.mylinearlayoutandroid:gravity="center"android:layout_height="fill_parent"android:layout_width="fill_parent"android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android">
- <com.androidtest.touch.test.mytestviewandroid:background="#FFFFFF"android:id="@+id/tv"android:layout_height="200px"android:layout_width="200px"android:text="leon"android:textcolor="#0000FF"android:textsize="40sp"android:textstyle="bold">
- </com.androidtest.touch.test.mytestview></com.androidtest.touch.test.mylinearlayout>
本文出自 “最牛傻蛋的博客” 博客,转载请与作者联系!
更多相关文章
- Android之SQLite学习
- 【Android-Error】Android(安卓)4.4.4 java.lang.NoClassDefFoun
- Android(安卓)Studio常用快捷键、Android(安卓)Studio快捷键大全
- Android线程优先级设置方法
- TextView支持的XML属性及相关方法
- Android和H5之间的交互
- drawRoundRect方法:绘制圆角矩形
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用