安卓TextView不可点击

4

我有一小段代码,开始使用相对布局后,TextView不再可点击。以下是XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/fav_list_selector"
 android:orientation="horizontal"
 android:padding="5dip">    
<ImageButton
    android:id="@+id/pinImage"
    android:layout_alignParentLeft="true"
    android:src="@drawable/ic_9_av_make_available_offline"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"/>    
<TextView
    android:id="@+id/favBusItem"
    android:layout_toRightOf="@+id/pinImage"
    android:layout_toLeftOf="@+id/transpoImage"
    android:layout_centerVertical="true"
    android:textSize="12sp"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"     
    android:clickable = "true"   
    android:textColor="#000000">        
</TextView>    
<ImageButton
    android:id="@+id/transpoImage"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/pinImage"
    android:src="@drawable/ic_9_av_make_available_offline"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"/>    
</RelativeLayout>

我怀疑TextView不可点击是因为ImageButton的缘故。

尝试在TextView标签中添加android:clickable="true" - Hugo Hideki Yamashita
是的,我尝试过了(它也在xml中)...不起作用。如果我将ImageButton更改为ImageView,它就可以工作。我不明白(ImageButton可能会出什么问题)。 - Guru
因为图像按钮充当按钮,而且您的文本视图位于其布局内,所以当您单击文本视图时,图像按钮单击事件将始终占据位置。如果您不使用图像按钮单击事件,则请将以下行添加到其布局中 android:clickable="false" android:focusable="false" - moh.sukhni
@Moh.Sukhni - 我尝试了你的解决方案(虽然我想让图像按钮可点击),但它没有起作用。 - Guru
我终于弄明白了。我需要另一个属性android:descendantFocusability="blocksDescendants",这对我有用。我将其添加到相对布局中。 - Guru
显示剩余4条评论
3个回答

4

我已经弄清楚了。无论如何,以下是答案,以防其他人遇到同样的问题:

为您的图像按钮添加此属性

 android:focusableInTouchMode="false"

也需要添加

标签


 android:descendantFocusability="blocksDescendants" 

对于您的父布局

这个方法对我很有效


0

在你的代码中,使用android:clickable="true"这一部分:

<ImageButton
android:id="@+id/transpoImage"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/pinImage"
android:src="@drawable/ic_9_av_make_available_offline"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>    

-1
在你的类中使用一个 OnClickListener,并将其连接到你的 TextView。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接