(Android)listview with multiple buttons, list item can't be clicked

你离开我真会死。 提交于 2019-11-27 18:33:52

问题


I have list with two buttons on it. When I want to click a list item it doesn't work, but my button is still clickable. How I can make all buttons include the entire list item to be clickable?

List item:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:mode="twoLine">

              <Button
                    android:id="@+id/erase"
                    android:layout_width="40dip"
                    android:layout_height="40dip"
                    android:focusable="false"
                    android:focusableInTouchMode="false"/>
              <ImageButton android:id="@+id/soundf"
                    android:layout_width="40dip"
                    android:layout_height="40dip"
                    android:focusable="false"
                    android:focusableInTouchMode="false"/> 
              <TextView android:id="@+id/texxt1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#CC0"/>
</TwoLineListItem>

Layout containing the ListView:

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <Button android:id="@+id/left" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="English to Indonesia"
            android:layout_weight="1" 
            android:background="@drawable/chbutt" />
        <Button android:id="@+id/right" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="Indonesia to English"
            android:layout_weight="1" 
            android:background="@drawable/chbutt" />
    </LinearLayout>

    <ListView android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        android:id="@+id/history"
        android:headerDividersEnabled="false"
        android:footerDividersEnabled="false"
        android:isScrollContainer="false" />
</LinearLayout>


回答1:


For Buttons, Checkboxs and ImageViews:

android:focusable="false"

Now both (buttons and rows) of ListView are clickable.

For ImageButtons, you have to set focusable while running, because the constructor of ImageButtons sets it to true. I recommend you using a ImageView instead of a ImageButton.




回答2:


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  android:id="@+id/rel1" android:layout_width="fill_parent"
        android:layout_height="wrap_content">

<Button
        android:id="@+id/erase"
        android:layout_marginLeft="6dip"
        android:layout_marginTop="6dip"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:background="@drawable/closebtn"
        android:focusable="false"
        android:focusableInTouchMode="false"
    />
   <ImageButton android:id="@+id/soundf"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_below="@+id/erase"
        android:layout_alignLeft="@+id/erase"
        android:background="@drawable/soundinv"
        android:focusable="false"
        android:focusableInTouchMode="false"
        /> 
   <TextView android:id="@+id/texxt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/erase"
        android:layout_alignTop="@+id/erase"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#CC0"
   />

   <TextView android:id="@+id/texxt2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/texxt1"
        android:layout_alignLeft="@+id/texxt1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFF"
   />
</RelativeLayout>

now you can also given click event to relative layout.




回答3:


Just add this to your Java code: holder.yourButton.setFocusable(false); I am using my own cursorAdapter so I am putting the line of code at the end of bindView this should do it.



来源:https://stackoverflow.com/questions/6184258/androidlistview-with-multiple-buttons-list-item-cant-be-clicked

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