[英]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 应用程序中,我有一个
MaterialCardView
包装了一个TextView
我希望是可复制的,例如我使用
android:textIsSelectable="true"
However, I also need to know when the MaterialCardView
is clicked as its within a RecyclerView
Item layout.但是,我还需要知道何时在
RecyclerView
项目布局中单击MaterialCardView
。
My onClickListener
for the MaterialCardView
is only triggered when I remove the android:textIsSelectable="true"
attribute from the child TextView
.仅当我从子
onClickListener
中删除android:textIsSelectable="true"
属性时,才会触发MaterialCardView
的TextView
。
here is the complete RecyclerView Item layout这是完整的 RecyclerView 项目布局
<?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?我怎样才能检测到对父
MaterialCardView
的点击并让子TextView
可选?
In your case (with textIsSelectable="true"
) the TextView
will still trigger an OnClick
event.在您的情况下(使用
textIsSelectable="true"
) TextView
仍将触发OnClick
事件。 So you could simply add an OnClickListener
to your TextView
and link that to the existing CardView click behavior.因此,您可以简单地将
TextView
OnClickListener
其链接到现有的 CardView 点击行为。
Would that cover your case?这会涵盖你的情况吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.