如何使 ChipGroup 发挥像 radioGroup 的作用?

ben*_*ter 4 java android android-layout android-chips material-components-android

如何使 a 的ChipGroup行为类似于radioButton在更改背景颜色时一次可以选择一个项目。

图片截图

我看到了类似这样的链接,但它对我没有帮助,因为我使用 alayoutInflater来显示我的芯片项目。

firebaseFirestore.collection("Categories").addSnapshotListener((queryDocumentSnapshots, e) -> {
            for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()){
                if (doc.getType() == DocumentChange.Type.ADDED){
                    Categories categories = doc.getDocument().toObject(Categories.class);
                    post_list.add(categories);
                    Chip chip = (Chip) getLayoutInflater().inflate(R.layout.chip_item_layout, chipGroup, false);
                    chip.setText(categories.getTitle());
                    chipGroup.addView(chip);
                    chipGroup.setOnCheckedChangeListener((chipGroup, id) -> {
                        Chip chip2 = ((Chip) chipGroup.getChildAt(chipGroup.getCheckedChipId()));
                        if (chip2 != null) {
                            for (int i = 0; i < chipGroup.getChildCount(); ++i) {
                                chipGroup.getChildAt(i).setClickable(true);
                                chip2.setChipBackgroundColorResource(R.color.customOrange);
                            }
                            chip2.setClickable(false);
                        }
                    });

                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 6

在您ChipGroup使用该app:singleSelection="true"属性时。通过这种方式,ChipGroup可以将其配置为一次仅允许检查单个芯片

<com.google.android.material.chip.ChipGroup
    app:singleSelection="true"
    ..>
Run Code Online (Sandbox Code Playgroud)

app:chipBackgroundColor然后您可以使用布局中的属性设置选择器颜色chip_item_layout.xml

就像是:

<com.google.android.material.chip.Chip
    style="@style/Widget.MaterialComponents.Chip.Choice"
    app:chipBackgroundColor="@color/chip_background_color"
    ..>
Run Code Online (Sandbox Code Playgroud)

注意style="@style/Widget.MaterialComponents.Chip.Choice"因为它定义芯片为android:checkable="true".

chip_background_color是一个选择器,您可以在其中定义不同状态下您最喜欢的颜色。这是默认选择器,您可以更改它:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- 24% opacity -->
  <item android:alpha="0.24" android:color="?attr/colorPrimary" android:state_enabled="true" android:state_selected="true"/>
  <item android:alpha="0.24" android:color="?attr/colorPrimary" android:state_enabled="true" android:state_checked="true"/>
  <!-- 12% of 87% opacity -->
  <item android:alpha="0.10" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface"/>

</selector>
Run Code Online (Sandbox Code Playgroud)

所选芯片由第一行 ( ) 中的颜色定义android:state_selected="true"

如果您想以编程方式执行此操作,只需使用(而不是OnCheckedChangeListenersetChipBackgroundColorResource方法中。

chip.setChipBackgroundColorResource(R.color.chip_background_color);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

另外,如果您希望至少需要一项选择,则可以使用该app:selectionRequired属性。该属性需要1.2.0(从1.2.0-alpha02