android - MaterialCardView:无法在 xml 布局中设置选中状态

标签 android material-design material-components-android materialcardview

安卓工作室 3.6

在 xml 布局中我有这个:

 <com.google.android.material.card.MaterialCardView
                android:id="@+id/cardPaymentCardView"
                style="@style/cardViewStyle"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:checkedIcon="@drawable/ic_credit_card_outline_select"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

要打开/关闭检查状态,我使用这个(在我的 Activity 中)

dataBinding.cardPaymentCardView.isChecked = !dataBinding.cardPaymentCardView.isChecked

而且它工作正常。不错。

但是我需要直接在xml中设置checked status。像这样:

android:checked_state="true"

但是编译出错

最佳答案

没有提供切换到检查状态的默认方式,客户端必须调用卡上的 setChecked(boolean)

支票卡

Cards implement Checkable interface. In the default style, @style/Widget.MaterialComponents.CardView, the checked state shows a checked icon and changes the overlay color. A default way of switching to checked state is not provided, clients have to call setChecked(boolean) on the card. Setter for an OnCheckedChangeListener is also provided.

MaterialCardView documentation

关于android - MaterialCardView:无法在 xml 布局中设置选中状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58644056/

相关文章:

Android Material 编辑文本 endIcon

android - 重启后如何重新设置闹钟

android - Android 5.0(和其他)上的设备所有者没有 root 设备,通过 NFC 进行设备配置

java - Android - 设置应用程序背景

android - AutoCompleteTextView float 提示

android - 更改对话框按钮颜色

java - 如何在 Rooted android 设备中使用 shell 命令读取短信?

javascript - Angular 中的 Material Design 不起作用

java - GWT Material - 设置下拉选择文本

android - 使用深色主题时如何使 Android CardView 可见