难道让家长可点击使所有的子元素可点击呢? [英] Does making parent clickable make all child element clickable as well?

查看:116
本文介绍了难道让家长可点击使所有的子元素可点击呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个的LinearLayout 有很多的子元素。当用户触摸任何这些子元素,相同的方法将被调用。为了不实现相同 onClickListener 的每一个元素,我实现了 onClickListener 父LinearLayout中的 ONLY

There is a LinearLayout with a lot of child elements. When a user touches any of those child elements, the same method will be invoked. In order not to implement the same onClickListener for each element, I implemented the onClickListener for the parent LinearLayout ONLY.

现在,当我点击父布局的边界内的任何地方,所需要的方法被调用,就像我已经实现了监听器所有子元素。

Now, when I click anywhere within the parent layout's borders, the desired method is being invoked just as I have implemented the listener for all child elements.

问:我可以依靠,每当我执行 onClickListener 父,所有的子元素会反应click事件

Q: Can I rely that anytime I implement onClickListener for the parent, all of its child elements will react to the click event?

问:什么会发生,如果任何子元素都有自己的 onClickListener ?会不会有该元素在碰撞或点击只会激发自己的单击事件?

Q: What would happen if any child element has its own onClickListener? Would there be a collision or clicking on that element would fire its own click event only?

推荐答案

您与您的第二个问题回答了你的第一个问题。甲clickEvent将交付给在布局层次的最低子元素。如果该元素没有一个onClick行为它将传递事件到其父直到事件被处理。

You answered your first question with your second question. A clickEvent will be delivered to the lowest child element in the layout hierarchy. If this element does not have an onClick behaviour it will pass the event up to its parent until the event gets handled.

因此​​,你可以治疗的LinearLayout作为您的onClick行为的一个单一的块。 如果你创建布局内的另一个可点击的元素一定要使它足够大,以减少缺少正确的项目用户的机会。

Therefore you can treat the LinearLayout as one single block for your onClick behaviour. If you create another clickable element inside the layout be sure to make it big enough to reduce the chance of the user missing the correct item.

这篇关于难道让家长可点击使所有的子元素可点击呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆