微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 中设置此值,而不是在图形视图中!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。