【问题标题】:TextView clickable is auto-enabling inside LinearLayout buttonTextView clickable 在 LinearLayout 按钮内自动启用
【发布时间】:2021-06-21 21:56:41
【问题描述】:

自从我将项目更新到 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"

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

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

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

【问题讨论】:

    标签: android textview android-linearlayout clickable android-11


    【解决方案1】:

    很简单,只需删除:

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

    从“LinearLayout”,它会干扰其他,并设置“TextView”:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      相关资源
      最近更新 更多