Android版式使所有孩子都无法点击 [英] Android Layout make all children's not clickable

查看:64
本文介绍了Android版式使所有孩子都无法点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Relative Layout 和其中的许多按钮以及TextViews等.除非发生某些事件,否则我想使所有按钮都不是 clickable .

I am using Relative Layout and many buttons in it with TextViews etc.I want to make all of them not clickable unless some event happens.

我尝试设置 RelativeLayout.setClickable(false); ,但是布局中的所有元素仍然是 clickable .

I tried setting RelativeLayout.setClickable(false); but still all the elements inside the layout are clickable.

我知道一种设置每个子元素不是 clickable 的方法,但是这不是一种合适的方法,因为我在布局中有很多子元素,例如按钮,文本视图等,我无法使每个子元素孩子无法点击.

I know one way of doing it that setting each child element not clickable but it is not an appropriate way because i have lot of child elements like buttons text views etc inside a layout i cannot make each child not clickable.

这是我的问题:如何在 layout 中将全部设置为 setClickable(false); ?

Here my question is How to set all to setClickable(false); in layout ??

推荐答案

当您说单击时,您实际上是在触摸吗?触摸事件是逐个元素完成的.首先将它们发送到顶层,该顶层将说明是否进行了处理,如果没有处理,则会进入视图的每个子级.

When you say click do you actually mean touch? Touch events are done element by element. They're first sent to the top level, which says if it handled it and if it didn't it goes onto each children of the view.

创建触摸事件时,将调用链中的 onTouch 视图方法,如果其中任何一个返回true(true表示我已经处理过!"),它将停止向下移动.孩子们.

When a touch event is created, the onTouch method of view in the chain is called, if any of these return true (true meaning "I handled this!") it stops going down to the children.

要让您的relativelayout块对其所有子级进行触摸和点击,您只需要设置onTouchListener即可,

To make your relativelayout block touches and clicks for all of its children you simply need to set the onTouchListener, like this:

YOUR_RELATIVE_LAYOUT.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // ignore all touch events
        return true;
    }
});

这将忽略相对布局(及其所有子项)上发生的所有触摸事件,其中包括简单的触摸然后释放事件(称为点击).

This will ignore all touch events happening on the relative layout (and all of its children) which includes simple touch down then release events (called clicks).

这篇关于Android版式使所有孩子都无法点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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