捕获带有 ImageButton 的 LinearLayout onClick 事件

发布于 2024-09-15 20:51:04 字数 1468 浏览 0 评论 0原文

我有一个 ImageButton 和一个 TextView 包裹在 LinearLayout 中,如下所示:

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="20" android:gravity="center"
        android:clickable="true" android:id="@+id/action_showhide">
        <ImageButton android:id="@+id/test"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:src="@drawable/ic_toggle_hide_states" android:background="@null"></ImageButton>
        <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/txtHide"
            android:textColor="@drawable/orange" android:textStyle="bold"></TextView>
    </LinearLayout>

ImageButton 由用于正常、聚焦和按下状态的自定义可绘制对象支持。我想允许用户单击 LinearLayout 中的任意位置来触发 OnClick 事件。下面的代码显示了 OnClickListener 的设置:

    final LinearLayout actionHide = (LinearLayout) findViewById(R.id.action_showhide);
    actionHide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(AppAdvocate.TAG, "Event caught");
        }
    });

当用户单击 TextView 上的任意位置时,代码起作用,但当用户单击 ImageButton 时,事件不会冒泡到 LinearLayout。我没有为按钮定义 onClickListener。我希望更改 ImageButton 的可绘制对象,因此我不想将其设置为 clickable=false。有没有办法让事件冒泡?

I have an ImageButton and a TextView wrapped in a LinearLayout like this:

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="20" android:gravity="center"
        android:clickable="true" android:id="@+id/action_showhide">
        <ImageButton android:id="@+id/test"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:src="@drawable/ic_toggle_hide_states" android:background="@null"></ImageButton>
        <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/txtHide"
            android:textColor="@drawable/orange" android:textStyle="bold"></TextView>
    </LinearLayout>

The ImageButton is backed by a custom drawable for normal, focused and pressed states. I would like to allow the user to click anywhere in the LinearLayout to fire an OnClick event. The code below shows the set up for the OnClickListener:

    final LinearLayout actionHide = (LinearLayout) findViewById(R.id.action_showhide);
    actionHide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(AppAdvocate.TAG, "Event caught");
        }
    });

The code works when the user clicks anywhere on the TextView but when the user clicks on the ImageButton the event doesn't bubble up to the LinearLayout. I am not defining an onClickListener for the button. I want the drawable for my ImageButton to change so I don't want to set it to clickable=false. Is there a way to bubble up the event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

呆橘 2024-09-22 20:51:04

为了使 LinearLayout 可点击,您需要在其所有子元素上设置 android:focusable="false"

In order to make LinearLayout clickable you need to set android:focusable="false" on all its child elements.

揪着可爱 2024-09-22 20:51:04

如果您确实不想让按钮不可点击,您可以向按钮添加一个侦听器并执行与 LinearLayout onClick 相同的操作。对于用户来说,它就像一个大按钮。

If you really don't want to make the button not clickable, you could just add a listener to the button and perform the same action as the LinearLayout onClick. It would appear to the user as if it was one big button.

你在看孤独的风景 2024-09-22 20:51:04

据我了解,您将需要以下内容:

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="20" android:gravity="center"
        android:clickable="true" android:id="@+id/action_showhide">
        <ImageButton android:id="@+id/test"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:src="@drawable/ic_toggle_hide_states" android:background="@null">         </ImageButton>
        <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/txtHide"
            android:textColor="@drawable/orange" android:textStyle="bold"
android:onClick="viewClicked"></TextView>
    </LinearLayout>

之后,您创建该函数:

public void viewClicked(View v){

    switch (v.getId())
    {
        case R.id.action_showhide:
            //do something
            break;
        case R.id.action_showhide:
            //do something
            break;
    }

}

from what I understand, you'll be needing the following:

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="20" android:gravity="center"
        android:clickable="true" android:id="@+id/action_showhide">
        <ImageButton android:id="@+id/test"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:src="@drawable/ic_toggle_hide_states" android:background="@null">         </ImageButton>
        <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/txtHide"
            android:textColor="@drawable/orange" android:textStyle="bold"
android:onClick="viewClicked"></TextView>
    </LinearLayout>

After that, you create the function:

public void viewClicked(View v){

    switch (v.getId())
    {
        case R.id.action_showhide:
            //do something
            break;
        case R.id.action_showhide:
            //do something
            break;
    }

}
橘味果▽酱 2024-09-22 20:51:04

在 ImageButton 上设置 android::clickable="false"。这应该有帮助。

Set android::clickable="false" on ImageButton. This should help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文