Android textIsSelectable stops TextView onClick working correctly(Android TextIsSelectable使TextView onClick无法正常工作)
问题描述
在我当前的Android应用程序中,我有一个MaterialCardView
包装了我希望是可复制的TextView
,例如我使用
android:textIsSelectable="true"
但是,我还需要知道MaterialCardView
在RecyclerView
项布局中被单击的时间。
仅当从子TextView
中删除android:textIsSelectable="true"
属性时,才会触发MaterialCardView
的onClickListener
。
以下是完整的RecEconerView项目布局
<?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
可选?
推荐答案
在您的情况下(使用textIsSelectable="true"
),TextView
仍然会触发OnClick
事件。因此,您只需将OnClickListener
添加到您的TextView
并将其链接到现有的CardView点击行为。
这能涵盖您的情况吗?
这篇关于Android TextIsSelectable使TextView onClick无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!