简体   繁体   中英

Android textIsSelectable stops TextView onClick working correctly

In my current Android application I have a MaterialCardView that wraps a TextView that I wish to be copyable eg I use

android:textIsSelectable="true"

However, I also need to know when the MaterialCardView is clicked as its within a RecyclerView Item layout.

My onClickListener for the MaterialCardView is only triggered when I remove the android:textIsSelectable="true" attribute from the child TextView .

here is the complete RecyclerView Item layout

<?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>

How can I both detect clicks on the Parent MaterialCardView and have the child TextView selectable?

In your case (with textIsSelectable="true" ) the TextView will still trigger an OnClick event. So you could simply add an OnClickListener to your TextView and link that to the existing CardView click behavior.

Would that cover your case?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM