我在按钮上有LinearLayout。有什么方法可以处理按钮点击事件?

时间:2019-09-20 12:12:47

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

我有一个android活动xml,他的xml里面有一个主要的RelativeLayout。里面的RelativeLayout添加了一个Button和一个linearlayout,我点击了按钮但没有点击,因为那个按钮过于线性了

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

单击以打印日志。

2 个答案:

答案 0 :(得分: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会将触摸事件发送到该触摸事件将遇到的第一个视图。 OnTouchListeners返回一个布尔值,指示他们消费事件的时间。如果他们没有使用该事件,则将其传播到触摸事件将遇到的层次结构中的下一个视图。

这就是为什么原来从未注册按钮的单击的原因,因为布局占用了touch事件并阻止其传播到按钮。

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

答案 1 :(得分:0)

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