Android的textIsSelectable属性会影响TextView的onClick事件正确性。

3
在我的当前 Android 应用中,我有一个包裹了一个 TextViewMaterialCardView,我希望能够将其设置为可复制的。例如,我使用:
android:textIsSelectable="true"

然而,我还需要知道何时单击了 RecyclerView 中的 MaterialCardView

只有在我从子 TextView 中删除 android:textIsSelectable="true" 属性时,MaterialCardViewonClickListener 才会被触发。

这是完整的 RecyclerView Item 布局:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_bibtext_cardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    card_view:cardBackgroundColor="?attr/colorSurface"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="5dp"
    card_view:cardPreventCornerOverlap="false"
    card_view:cardUseCompatPadding="true"
    card_view:contentPadding="10dp">

    <TextView
        android:id="@+id/bibtex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textIsSelectable="true"
        android:background="?android:attr/selectableItemBackground"
        android:textColor="?android:attr/textColorPrimary" />

</com.google.android.material.card.MaterialCardView>

我怎样可以在父亲MaterialCardView上检测点击事件时,又让子元素TextView可选?
1个回答

1
在您的情况下(使用 textIsSelectable="true"),TextView 仍会触发 OnClick 事件。因此,您可以简单地为您的 TextView 添加一个 OnClickListener 并将其链接到现有的 CardView 点击行为。
那能满足您的需求吗?

1
这确实允许我捕获TextView的点击事件,但用户必须点击两次才能“触发”点击事件。 - Hector

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