开发者

Capturing LinearLayout onClick events with ImageButton inside it

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 L开发者_开发技巧inearLayout 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?


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


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:

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

}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜