LinearLayout's click listener is never called

限于喜欢 提交于 2019-12-07 03:26:51

问题


Trying to get an onclick listener working on a linearlayout but its never called :(. Have enabled clickable and focsuable (both modes) and still cant get the click listener to respond. Platform details: Android 3.0.. Any help?? Code below

           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/menu_items_button"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:gravity="center_horizontal"
                android:paddingTop="@dimen/gen_margin_xsmall"
                android:paddingBottom="@dimen/gen_margin_xsmall"
                android:background="@drawable/rule_bg_menu_button"
                android:clickable="true"
              android:focusableInTouchMode="true"
            >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/menu_items"
                    android:tag="image"
                />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:tag="text"
                    android:text="@string/menu_items_icon_txt"
                    style="@style/textDisplay.mediumLarge"
                />

            </LinearLayout>

and in the code to add the event listener

_itemsButton = (LinearLayout) menu.findViewById(R.id.menu_items_button);
final Intent itemsIntent = new Intent(this, ItemsActivity.class);
_itemsButton.setOnClickListener(
            new View.OnClickListener() {    
                @Override
                public void onClick(View v) {
                    startActivity(itemsIntent); //Never called!
                }
            }
        );

The reason I'm doing this and not using an Image button instead is because the background of the "button" is state based (gradient changes) but also the image and in order to combine to the two on click / on focus, I used a linearlayout which has an ImageView in itself.. any suggestions on why the clickListener is not working on the linearLayout?

thx


回答1:


Did the click go to the ImageView instead of the LinearLayout? Try clicking in the pad area (if any) or try putting the click listenner on the ImageView1.




回答2:


(adding my response as a new answer so that I can use the PRE tag.)

The easy way is to set the same click listener on the image view and the text view.

View.OnClickListener activityLauncher = new View.OnClickListener() {... }
layout.setOnClickListener(activityLauncher);
imageView.setOnClickListener(activityLauncher);
textView.imageView.setOnClickListener(activityLauncher);



回答3:


The width of your LinearLayout is set to "0dip", you should see nothing on the screen.

If the width is changed to "FULL_PARENT", that works. Please check your code carefully again.



来源:https://stackoverflow.com/questions/5651894/linearlayouts-click-listener-is-never-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!