gpt4 book ai didi

android - 使用其中的 ImageButton 捕获 LinearLayout onClick 事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:07:10 26 4
gpt4 key购买 nike

我有一个 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。有没有办法让事件冒泡?

最佳答案

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

关于android - 使用其中的 ImageButton 捕获 LinearLayout onClick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3570605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com