繁体   English   中英

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

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

我有一个 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: ");
            }
        });

点击打印日志。

尝试这个:

<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 视图像堆栈一样创建,其中 xml 文件中较低的视图呈现在文件中较早的视图之上。

听起来您的线性布局正在拦截触摸事件,以防止它们传播到其下方的按钮。

检查这一点的一种方法是在线性布局上设置背景,您会看到当它具有彩色背景时,按钮不可见。

更新:

由于需要允许按钮和布局的触摸事件,我将扩展我的解决方案。

OnClickListeners 是在 android 视图上接收点击事件的标准方法。 然而,在下面有OnTouchListeners处理视图上的原始触摸而不是单击事件。

当 android 屏幕以任何方式被触摸时,该事件被记录下来,并且 android 将向触摸事件将遇到的第一个视图发送一个触摸事件。 OnTouchListeners 返回一个 boolean 指示他们是否消费了该事件。 如果他们没有消费该事件,那么它将传播到触摸事件将遇到的层次结构中的下一个视图。

这就是为什么您的按钮的点击按钮最初从未被注册,因为布局正在消耗触摸事件并阻止它传播到按钮。

这是标准的 android 行为,只有一个视图可以处理任何单个触摸事件。

不这样做的决定表明您应该考虑应用程序的设计并决定这种行为是否绝对必要。

但是,如果您的情况是我将提供的罕见情况之一,并且是关于触摸侦听器的示例。

请注意,此触摸监听器假定您使用的是我在上面发布的 xml 以及附加到原始问题中按钮的点击监听器。

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

然而,这将在用户触摸布局和释放时再次触发,因为这是两个单独的运动事件。

要仅触发一种类型的运动事件,您需要检查传递的事件。

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;
                }
            });

在此代码中,仅当用户从视图中抬起手指时才会出现日志,因为ACTION_UP是用户抬起手指的事件动作。

相对布局与线性布局相同,它保留了添加到其中的元素的顺序。 您首先添加了按钮,然后添加了线性布局,这就是为什么按钮已低于线性布局,因此您无法单击它。 讲述按钮视图上方的线性布局,您将看到按钮并通过 java 编码完成剩余部分

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM