【问题标题】:I have LinearLayout over a button. Is there any way to handle underneath button click event?我在一个按钮上有 LinearLayout。有没有办法处理按钮点击事件?
【发布时间】:2020-01-21 12:01:40
【问题描述】:

我有一个 android 活动 xml 和他的 xml 在里面做 main RelativeLayout 。那个 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: ");
            }
        });

点击打印日志。

【问题讨论】:

    标签: java android view android-linearlayout touch-event


    【解决方案1】:

    试试这个:

    <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 会向触摸事件遇到的第一个视图发送一个 on touch 事件。 OnTouchListeners 返回一个布尔值,指示他们是否消费了该事件。如果他们没有消费该事件,那么它会传播到触摸事件将遇到的层次结构中的下一个视图。

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

    这是标准的 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 是用户抬起手指的事件动作。

    【讨论】:

    • 您的代码是正确的,但我想触摸线性布局和按钮。我想同时管理两个触摸。
    • 我已经更新了我的答案,包括一个关于使用触摸监听器的部分。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多