捕捉包含ImageButton的LinearLayout的onClick事件

6

我有一个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>
是由自定义的可绘制图形支持正常、聚焦和按下状态。我希望允许用户在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。我没有为按钮定义onClickListner。我想要我的ImageButton的drawable更改,因此我不想将其设置为clickable=false。有没有一种方法可以使事件冒泡?

4个回答

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

1
我将所有子元素的 "android:focusable=false" 属性设置为 false,但 LinearLayout 仍然无法点击。 - bowman han

0

根据我的理解,您将需要以下内容:

    <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;
    }

}

0

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


2
不,我不想将它设置为clickable=false。 - thriqon

0

如果你真的不想让按钮变得不可点击,你可以向按钮添加一个监听器,并执行与LinearLayout onClick相同的操作。对于用户来说,它看起来就像是一个大按钮。


谢谢Mayra,这个有效。我应该如何将LinearLayout的onClick事件级联到ImageButton,以便在按下LinearLayout时更改其可绘制状态? - Richard
我不确定你指的是哪个状态。 图像按钮是否像切换按钮一样,显示已点击的状态? 您应该能够在切换按钮上调用setChecked(checkedState)。 - Cheryl Simon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接