条件判断的内部调用了该View的dispatchTouchEvent() // 即 实现了点击事件从ViewGroup到子View的传递(具体请看下面章节介绍的View事件分发机制..." android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http...结果分析 点击Button时,因为ViewGroup默认不拦截,所以事件会传递到子View Button,于是执行Button.onClick()。...点击空白区域时,ViewGroup. dispatchTouchEvent()里遍历所有子View希望找到被点击子View时找不到,所以ViewGroup自身会处理该事件,于是执行onTouchEvent...黑线:ACTION_DOWN事件传递方向 红线:ACTION_MOVE、ACTION_UP事件传递方向 6.2 onTouch()和onTouchEvent()的区别 该2个方法都是在View.dispatchTouchEvent
" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation...="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android...android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button2...分别点击一下Button1、Button2和空白区域,打印结果如下所示: ? 你会发现,当点击按钮的时候,MyLayout注册的onTouch方法并不会执行,只有点击空白区域的时候才会执行该方法。...你会发现,不管你点击哪里,永远都只会触发MyLayout的touch事件了,按钮的点击事件完全被屏蔽掉了! 看到这里,我相信你对这个函数应该已经的差不多了,
="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@+id/relativeLayout1..." android:layout_width="fill_parent" android:layout_height="fill_parent"> onTouch返回值 1 、如果return false 说明还没有消费onTouch事件,在执行onTouch里代码后,onTouch事件并没有结束。...2、如果return true 说明消费了onTouch事件 onTouch事件结束了 但在实际操作中 除了ACTION_DOWN事件以外,其余的事件只有返回true的那个方法才能捕捉到。...所以 返回false的时候只能捕捉到每次的第一个DOWN事件 后面的MOVE 和UP事件就捕捉不到了。
条件判断的内部调用了该View的dispatchTouchEvent() // 即 实现了点击事件从ViewGroup到子View的传递(具体请看下面章节介绍的View事件分发机制..." android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http...结果分析 点击Button时,因为ViewGroup默认不拦截,所以事件会传递到子View Button,于是执行Button.onClick()。...点击空白区域时,ViewGroup. dispatchTouchEvent()里遍历所有子View希望找到被点击子View时找不到,所以ViewGroup自身会处理该事件,于是执行onTouchEvent...黑线:ACTION_DOWN事件传递方向 红线:ACTION_MOVE、ACTION_UP事件传递方向 [image.png] 6.2 onTouch()和onTouchEvent()的区别 该2个方法都是在
还有就是在自定义控件的时候,需要处理一些事件时候,也必须把逻辑处理好,父 view 和子view 都需要接收事件,然后处理。如果不明白事件传递机制,很难开发出需要的效果。...下面点击button以外的部分,输出如下图中的第二个矩形,和之前没有button一样。...当把CustomButton中的onTouchEvent返回改为true的情况下,其实是和默认情况下一样的,这也说明默认情况下button消费了点击事件。...= "match_parent" android:layout_height= "match_parent" android:paddingBottom= "@dimen/activity_vertical_margin...进行消费; 如果当前 View 获取的事件来自外层父控件,则会将事件返回给父 View 的 onTouchEvent 进行消费。
顾名思义,ViewGroup就是一组View的集合,它包含很多的子View和子VewGroup,是Android中所有布局的父类或间接父类,像LinearLayout、RelativeLayout等都是继承自..." android:layout_width="match_parent" android:layout_height="match_parent" android:orientation...android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="...分别点击一下Button1、Button2和空白区域,打印结果如下所示: ? 你会发现,当点击按钮的时候,MyLayout注册的onTouch方法并不会执行,只有点击空白区域的时候才会执行该方法。...我们也因此证实了,按钮点击事件的处理确实就是在这里进行的。 然后需要注意一下,调用子View的dispatchTouchEvent后是有返回值的。
="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" >...<TextView android:id="@+id/textView" android:layout_width="match_parent" android...="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" >...<TextView android:id="@+id/textView" android:layout_width="match_parent" android...="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" >
onDraw 和 onTouch,而自定义 ViewGroup 还要考虑子 View 的测量、子 View 的布局、子 View 的事件分发等等,涉及到的方法了 onMeasure、onLayout、...(2)获取子 View 通过上面的分析我们知道一共有三个子 View:左侧菜单、中间主体、右侧菜单,但是这三个子 View 不一定全有,如果用户只配置了左侧菜单,那右侧菜单子 View 就不存在。...b、当菜单打开(左右菜单都一样)的时候,点击中间主体区域时拦截事件,点击其它地方不拦截事件。也就是说当菜单打开时,主体区域的页面元素不可点击,列表也不可滑动,但是菜单区域的元素可以点击。...是否点击在中间主体区域稍微复杂一点,我们首先要获取手指点击相对于屏幕的坐标值。...="match_parent" android:layout_height="match_parent" android:orientation="horizontal"
="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools...-- 抽屉 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent...父View和子View设同一个OnTouchListener。但是只有父view来处理触摸事件。...如果是子view接收到了触摸事件,则做一个bool标记firstOnTv = true,返回false,把触摸事件交给父view来处理。...父view处理触摸事件时,判断如果刚才点中的是子view(即mContentTv),则在MotionEvent.ACTION_MOVE时更改子view的坐标。
,然后判断子元素是否在播放动画和点击事件是否落在了子元素的区域内。...在 MotionEvent 事件中分别会在 up 和 down 中会执行点击 onClick 和 onLongClick 回调。...到这里点击事件的分发机制源码实现已经分析完了,结合之前分析的传递规则和下面这张图,然后结合源码相信你应该理解了事件分发跟事件处理机制了。 !...="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">...android:layout_width="match_parent" android:layout_height="match_parent"> </com.devyk.customview.sample
="fill_parent" android:layout_height="fill_parent" android:id="@+id/mylayout"> layout_width="match_parent" android:layout_height="wrap_content" android:text=...现在来分析下上面的打印结果;在1中,当我们点击Button时会先触发onTouch事件(之所以打印action为0,1各一次是因为按下抬起两个触摸动作被触发)然后才触发onClick事件;在2中也同理类似...="fill_parent" android:layout_height="fill_parent" android:id="@+id/mylayout"> layout_width="match_parent" android:layout_height="wrap_content" android
匿名内部类作为事件监听器: 原理和上面一个方法相同,但是可以用Java的语法简化,参照Java匿名内部类。...长按事件的返回值是boolean而不是void,所以最后要返回 true或 false。 返回值的含义: 当我们长按时,会触发长按操作,那么如果我们同时也设置了点击监听,我们会不会也触发了点击监听?...触摸事件 触摸事件有onTouch方法有参数MotionEvent event,通过对象event方法getX()和getY()可以获取触摸出横纵坐标。...="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin...="25dp"/> layout_width="match_parent" android:layout_height="wrap_content
(3)MotionEvent.ACTION_UP 与down对应,表示抬起 另外,明确事件传递机制的最终目的都是为了触发执行View的点击监听和触摸监听: ******.setOnClickListener...="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin...为了更清晰这个流程,下面作如下改动: 1、如果事件传给了孩子们,但孩子没有onTouch和onClick监听怎么办?...因为事件给了孩子们,它没监听也关系不到父亲了,父亲的onClick和onTouch都没执行。...果然事件就此打住,孩子们压根不知道,父亲执行了onClick和onTouch监听。可见父亲还是伟大的啊,只要谋士不拦截事件,那么事件就给孩子。
true 表示拦截这个事件,不继续分发给子视图,并调用自身的onTouchEvent进行消费 false或者super.onInterceptEvent表示不对事件进行拦截,需要继续传递给子视图 消费(...则事件不会传递给子view false货super.onInterceptTouchEvent,事件会继续传递给子view 3、在子view中对事件进行了消费,viewGroup将接受不到任何事件...ACTION_UP也需要返回false,如果返回true,并且滑动事件交给子View处理,那么子View将接收不到ACTION_UP事件,子View的onClick事件也无法触发。...标志位控制,一旦父容器拦截ACTION_DOWN那么所有的事件都不会传递给子View b、滑动策略的逻辑放在子View的dispatchTouchEvent方法的ACTION_MOVE中,如果父容器需要获取点击事件则调用...parent.requestDisallowInterceptTouchEvent(false)方法,让父容器去拦截事件。
onTouchEvent (ev) ; } else { //如果onInterceptTouchEvent()返回false则代表当前View不拦截点击事件 //则该点击事件则会继续传递给它的子元素...View 的 onTouchEvent 默认都会消耗事件(返回true),除非它是不可点击的(clickable和longClickable同时为false).View 的 longClickable...,并且它收到了 down 和 up 的事件。...="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools...手指 -> 点击Button,再点击 空白处(即ViewGroup),观察日志打印: 可以发现,viewGroup已经消费了此次事件,这时无论点击什么位置,事件都不会传递到子view。
就能顺利的传递到子控件了,因此这就是为什么Button点击后能够收到ACTION_UP,而对于TextView而言,这里就不一样了,由于TextView是不可点击的,因此在执行View的onTouchEvent...="match_parent" android:layout_height="match_parent" tools:context="com.lhd.touchevent.MainActivity...app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent..." app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf...: ViewGroup中的dispatchTouchEvent方法,负责把事件分发给子View,如果没有子View,就自己处理了 ViewGroup的onInterceptTouchEvent方法,是否拦截触摸事件
="match_parent" android:layout_height="match_parent" android:layout_margin="16dp" android...="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/view"...="match_parent" android:layout_height="20dp" android:layout_above="@+id/id_tv_bottom"...="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true"...事件实现点击外部消失 加载自定义的xml文件,然后获取ListView组件,设置adapter 即可显示UI布局。
="match_parent" android:layout_height="match_parent" android:id="@+id/layout"> layout_width="match_parent" android:layout_height="wrap_content"...完全符合上面点击Button结果分析的猜想。 那接下来还是要看看Activity里关于这几个方法的源码了。...若Activity下面的子view拦截了touchevent事件(返回true)则Activity.onTouchEvent方法就不会执行。...mWindow.shouldCloseOnTouch(this, event)中的mWindow实际就是上面分析dispatchTouchEvent方法里的getWindow()对象,所以直接到Window抽象类和PhoneWindow
长按和点击事件存在产生的问题 如果我们一个事件同时绑定了OnClick事件和OnLongClick事件,会发生什么!...再来看一个有趣的动作,可以移动的按钮,根据容器的event获取 X和Y坐标给按钮 现在打开MainActivity.xml的给容器一个ID值 parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin...但是Android得到焦点和失去焦点都在同个方法内运行 随便拖一个TextBox在安卓里面叫EditText 输入事件代码 EditText editText = (EditText) findViewById...当然我们也可以根据keyCode去获取点击了哪个键!实际上非常有趣...
领取专属 10元无门槛券
手把手带您无忧上云