捕获带有 ImageButton 的 LinearLayout onClick 事件
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了使
LinearLayout
可点击,您需要在其所有子元素上设置android:focusable="false"
。In order to make
LinearLayout
clickable you need to setandroid:focusable="false"
on all its child elements.如果您确实不想让按钮不可点击,您可以向按钮添加一个侦听器并执行与 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.
据我了解,您将需要以下内容:
之后,您创建该函数:
from what I understand, you'll be needing the following:
After that, you create the function:
在 ImageButton 上设置 android::clickable="false"。这应该有帮助。
Set android::clickable="false" on ImageButton. This should help.