簡體   English   中英

如何使 ChipGroup 像 radioGroup 一樣工作?

[英]How do I make a ChipGroup to act like a radioGroup?

如何使ChipGroup像一個radioButton一樣,可以在更改背景顏色的同時一次選擇一個項目。

圖片截圖

我看到了類似這樣的鏈接,但它對我沒有幫助,因為我正在使用layoutInflater來顯示我的芯片項目。

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);
                        }
                    });

                }
            }
        });

在您的ChipGroup使用app:singleSelection="true"屬性。 這樣, ChipGroup可以配置為一次只允許檢查一個芯片

<com.google.android.material.chip.ChipGroup
    app:singleSelection="true"
    ..>

然后,您可以使用布局chip_item_layout.xml中的app:chipBackgroundColor屬性設置選擇器顏色。

就像是:

<com.google.android.material.chip.Chip
    style="@style/Widget.MaterialComponents.Chip.Choice"
    app:chipBackgroundColor="@color/chip_background_color"
    ..>

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

chip_background_color是一個選擇器,您可以在其中定義您最喜歡的 colors 在不同的狀態。 它是默認選擇器,您可以更改它:

<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>

所選芯片由您的情況下的顏色定義為第一行( android:state_selected="true" )。

如果您想以編程方式執行此操作,只需使用(不在OnCheckedChangeListener中) setChipBackgroundColorResource方法。

chip.setChipBackgroundColorResource(R.color.chip_background_color);

在此處輸入圖像描述

此外,如果您想要求至少一項選擇,您可以使用app:selectionRequired屬性。 此屬性需要 1.2.0(從1.2.0-alpha02開始)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM