在我当前的 Android 应用程序中,我有一个 MaterialCardView
,它包装了一个我希望可复制的 TextView
,例如我用
android:textIsSelectable="true"
但是,我还需要知道 MaterialCardView
何时被点击,因为它位于 RecyclerView
项目布局中。
我的 MaterialCardView
的 onClickListener
仅在我从子 中删除
.android:textIsSelectable="true"
属性时触发TextView
这是完整的 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
可选择?
最佳答案
在您的情况下(使用 textIsSelectable="true"
),TextView
仍会触发 OnClick
事件。因此,您可以简单地将 OnClickListener
添加到您的 TextView
并将其链接到现有的 CardView 点击行为。
这可以涵盖您的情况吗?
关于Android textIsSelectable 停止 TextView onClick 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65123492/