6

我有一个列表,里面有两个按钮。当我想单击一个列表项时,它不起作用,但我的按钮仍然可以单击。 如何使所有按钮都包含整个列表项可点击?

项目清单:

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

包含 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>
4

3 回答 3

29

对于ButtonsCheckboxsImageViews

android:focusable="false"

现在 ListView 的两个(按钮和行)都是可点击的。

对于ImageButtons,您必须在运行时设置可聚焦,因为 ImageButtons 的构造函数将其设置为 true。我建议您使用 ImageView 而不是 ImageButton。

于 2012-03-23T15:15:35.327 回答
0
<?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>

现在您还可以将点击事件赋予相对布局。

于 2011-05-31T07:32:29.290 回答
0

只需将其添加到您的 Java 代码中:holder.yourButton.setFocusable(false); 我正在使用我自己的 cursorAdapter,所以我将代码行放在末尾bindView 应该这样做。

于 2013-05-21T06:28:15.040 回答