TextView 可点击在 LinearLayout 按钮内自动启用

问题描述

自从我将我的项目更新到 30 级和 android studio 到 4.2 以来,我一直遇到一个非常烦人的问题。我所有的可点击视图都有一个奇怪的问题,我有一些线性布局、相对布局设置为可点击和一个 onClickListener,这在此更新之前完美无缺。

但是现在当我尝试单击此布局按钮之一时,我只能单击视图的外部而不是任何子组件,例如:

<LinearLayout
    android:id="@+id/basket_expand"
    android:clickable="true"
    android:focusable="true"
    android:padding="12dp"
    android:focusableInTouchMode="true"
    android:background="@drawable/rounded_green"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical">

    <TextView
        android:background="@drawable/dark_rounded_green"
        android:padding="5dp"
        android:id="@+id/order_items"
        android:enabled="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/zero"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/basket_button_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:clickable="false"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:text="@string/open_basket"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/order_total"
        android:layout_width="wrap_content"
        android:gravity="end"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="15sp"
        android:text="$0"
        tools:ignore="HardcodedText" />
</LinearLayout>

在此视图中,我无法单击三个文本,只是当我单击线性布局的边框时,单击将触发,单击 textViews 时,我可以看到在 Profiler 中调用一个按下事件,但未调用父 onClick 事件.

我检查了布局检查器,尽管它仍然显示为启用的文本视图的硬编码可点击错误

来自 xml:

android:clickable="false"

在布局检查器中运行应用程序时:

enter image description here

在我的 kotlin 代码或任何其他部分中,我没有将可点击设置为 true 的地方。我试过启用和禁用一些属性,但没有任何变化,例如descendantFocusability,将clickable 和focusable 设置为false 给孩子,似乎没有任何效果。任何想法为什么会发生这种情况?谢谢。

编辑:如果我单击一个随机按钮并添加到 onclick 侦听器 isClickable false 到 textviews 父视图单击工作,但这真的很麻烦,它不会在 recyclerview (发生这种情况的一个测试用例)

解决方法

很简单,只需删除:

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

从“LinearLayout”中,它会干扰其他人,并使用以下方法设置“TextView”:

android:clickable="true"
android:focusable="true"

还有一件事,您的渲染可能无法正常工作,因此请尝试在 xml 中设置此值,而不是在图形视图中!

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...