开发者

LinearLayout (with onClick) containing ImageButton - is there a better way to solve it?

At the moment I have something like that:

开发者_Go百科
<LinearLayout android:layout_height="80dip" android:id="@+id/linearLayoutSettings"
 android:layout_width="80dip" android:orientation="vertical">

    <ImageButton android:layout_height="wrap_content" android:src="@android:drawable/ic_menu_manage" 
android:id="@+id/imageButton1" android:layout_width="wrap_content"></ImageButton>

</LinearLayout>

LinearLayout has onClick listener attached to it.

Problem: When a ImageButton is clicked inside the LinearLayout, the event doesn't get triggered.

I could solve it by attaching the same on click to this button as I attached to LinearLayout. But in that case it would mean a lot of repetetive code (have many buttons).

Question: Is there a more effective way to solve this problem?


Change the ImageButton to an ImageView and you will start getting the click event


Attach the following tag to each imagebutton:
android:onClick="yourOnClickFunction"
Then, in your activity, add a corresponding function:

public void yourOnClickFunction(final View v) {
switch(v.getId()) {
     //Do whatever is necessary.
}
}

In the switch-block you need to know about the buttons IDs. You can get them via findViewById(R.id.aButton).


You can write loop finding all buttons in layout and attach once created listener to all of them.


If I understand correctly, when the button is clicked, the LinearLayout's onClickListener doesn't fire. Which makes sense.

What you can do is put a yourLinearLayout.performClick(); which will programatically fire the layouts onClick event. If you put it at the end of the buttons onClick then it will perform the buttons, then the layouts, in order.


you must set the attribute setClickable(true);!! you can access it in xml using android:clickable="true"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜