使父元素可点击是否也会使所有子元素也可点击? [英] Does making parent clickable make all child element clickable as well?

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

问题描述

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

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 时,它的所有子元素都会对点击事件做出反应?

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天全站免登陆