简体   繁体   English

我在一个按钮上有 LinearLayout。 有没有办法处理按钮点击事件?

[英]I have LinearLayout over a button. Is there any way to handle underneath button click event?

i have one android activity xml and his xml inside make main RelativeLayout.that RelativeLayout inside add one Button and one linearlayout and i have clicking on button but not click,because of that button overly linear layout我有一个 android 活动 xml 和他的 xml 里面是主要的 RelativeLayout.that RelativeLayout 里面添加了一个按钮和一个线性布局而不是点击按钮,我有过度点击按钮但不是点击的线性布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </LinearLayout>

</RelativeLayout>

getActivity().findViewById(R.id.btnTest).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: ");
            }
        });

click to print log.点击打印日志。

Try this:尝试这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </LinearLayout>

    <Button
        android:id="@+id/btnTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Android xml views are created like a stack where views lower in the xml file are rendered on top of those earlier in the file. Android xml 视图像堆栈一样创建,其中 xml 文件中较低的视图呈现在文件中较早的视图之上。

It sounds like your linear layout is intercepting touch events preventing them from being propagated to the button underneath it.听起来您的线性布局正在拦截触摸事件,以防止它们传播到其下方的按钮。

A way to check this is to set a background on the linear layout, you'll see that when it has a coloured background the button is not visible.检查这一点的一种方法是在线性布局上设置背景,您会看到当它具有彩色背景时,按钮不可见。

Update:更新:

Since there is a requirement to allow touch events for both the button and the layout I'll extend my solution.由于需要允许按钮和布局的触摸事件,我将扩展我的解决方案。

OnClickListeners are the standard method for receiving tap events on android views. OnClickListeners 是在 android 视图上接收点击事件的标准方法。 However lower down there are OnTouchListeners which handle raw touches on a view rather than click events.然而,在下面有OnTouchListeners处理视图上的原始触摸而不是单击事件。

When the android screen is touched in any way the event is recorded and android will send an on touch event to the first view that touch event would encounter.当 android 屏幕以任何方式被触摸时,该事件被记录下来,并且 android 将向触摸事件将遇到的第一个视图发送一个触摸事件。 OnTouchListeners return a boolean indicating whetehr they consumed the event. OnTouchListeners 返回一个 boolean 指示他们是否消费了该事件。 If they did not consume the event then it is propagated to the next view down in the hierarchy which the touch event would encounter.如果他们没有消费该事件,那么它将传播到触摸事件将遇到的层次结构中的下一个视图。

This is why your button's on click was originally never being registered as the layout was consuming the touch event and preventing it from being propagated to the button.这就是为什么您的按钮的点击按钮最初从未被注册,因为布局正在消耗触摸事件并阻止它传播到按钮。

This is the standard android behaviour, only one view will handle any single touch event.这是标准的 android 行为,只有一个视图可以处理任何单个触摸事件。

A decision to do otherwise is a sign that you should consider the design for your application and decide if this behaviour is definitely necessary.不这样做的决定表明您应该考虑应用程序的设计并决定这种行为是否绝对必要。

However, in case your circumstances are one of the rare ocassions I'll provide and example on touch listener.但是,如果您的情况是我将提供的罕见情况之一,并且是关于触摸侦听器的示例。

Please note, this on touch listener assumes that you are using the xml I posted above as well as he on click listener attached to the button in the original question.请注意,此触摸监听器假定您使用的是我在上面发布的 xml 以及附加到原始问题中按钮的点击监听器。

getActivity()
    .findViewById(R.id.parentLayout)
    .setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.e(TAG, "onTouch: ");
                return false;
            }
        });

This however will trigger both when user touches the layout and again when they release as those are two seperate motion events.然而,这将在用户触摸布局和释放时再次触发,因为这是两个单独的运动事件。

To trigger only on one type of motion event you will need to examine the passed event.要仅触发一种类型的运动事件,您需要检查传递的事件。

getActivity()
        .findViewById(R.id.parentLayout)
        .setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                       Log.e(TAG, "onTouch: ");
                    }
                    return false;
                }
            });

In this code the log will only occur when the user lifts their finger from the view as ACTION_UP is the event action for a user lifting their finger.在此代码中,仅当用户从视图中抬起手指时才会出现日志,因为ACTION_UP是用户抬起手指的事件动作。

The relative layout is same as Linear Layout which preserves the order of elements added inside it.相对布局与线性布局相同,它保留了添加到其中的元素的顺序。 you added Button first and Linear Layout second thats why button has gone below the linear layout hence you are not able to click on it.您首先添加了按钮,然后添加了线性布局,这就是为什么按钮已低于线性布局,因此您无法单击它。 tale the linear layout above the button view and you will see the button and do the remaing part by java coding讲述按钮视图上方的线性布局,您将看到按钮并通过 java 编码完成剩余部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮更改图标。 爪哇 - Change icon with click on button. Java 从表格视图处理按钮单击事件 - Handle Button click event from Table view Click() 事件在按钮上不起作用 (XPATH) - Click() event not working over a button (XPATH) 每次单击按钮将视图添加到Fragment的LinearLayout中 - Add Views to LinearLayout of Fragment everytime I click button 当我点击保存按钮时没有输入任何内容时应用程序崩溃。 应该弹出错误 - App crashes when nothing is inputted when I click save button. Error should pop up 我想检查onClick按钮事件的蓝牙状态。 有可能这样做吗? 这是我的代码的一部分 - i want to check the bluetooth state on the event of onClick button. is it possible to do so? here are part of my code 在我的android项目中,有两个类,并且在第一类上有一个按钮,当我单击按钮时,我需要查看下一页。 - In my android project there are two classes and there is a button on first class,i need to view next page when i click on the button. 如何将 chrome 浏览器缩放到 67% 并单击下一步按钮。? - How to zoom the chrome Browser to 67% and click on the next button.? 从按钮单击访问LinearLayout子类参数 - Access LinearLayout Subclass Parameter from Button Click 如何处理表格视图中的按钮单击事件以创建超链接 - How to handle Button click event from Table view to create Hyperlink
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM