【问题标题】:How do I make a ChipGroup to act like a radioGroup?如何使 ChipGroup 像 radioGroup 一样工作?
【发布时间】:2019-10-05 05:52:42
【问题描述】:

如何使ChipGroupradioButton 一样在更改背景颜色时一次选择一个项目。

我在 this link 看到了类似的东西,但它对我没有帮助,因为我使用 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);
                        }
                    });

                }
            }
        });

【问题讨论】:

    标签: java android android-layout material-components-android android-chips


    【解决方案1】:

    在您的ChipGroup 中使用app:singleSelection="true" 属性。通过这种方式,ChipGroup 可以配置为一次只允许检查一个芯片

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

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

    类似:

    <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 是一个选择器,您可以在其中定义不同状态下您最喜欢的颜色。它是默认选择器,您可以更改它:

    <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 中的notsetChipBackgroundColorResource 方法。

    chip.setChipBackgroundColorResource(R.color.chip_background_color);
    

    此外,如果您想要求至少一项选择,您可以使用 app:selectionRequired 属性。该属性需要1.2.0(从1.2.0-alpha02开始)

    【讨论】:

    • 我希望芯片组像一个单选按钮一样,在曾经按下的芯片项目上具有背景颜色,从而在单击项目时产生切换效果。
    • 这将为所有芯片项设置背景颜色
    • 您在寻找什么并不完全清楚。如果您想在ChipGroup 中进行单个选择,请使用app:singleSelection="true" 属性。如果您想为不同的状态(选择、启用、正常)自定义颜色,您必须为每个 Chip 设置选择器颜色。
    • @bensofter 这将为所有芯片项设置背景颜色。是的,但它是一个选择器,而不仅仅是一种颜色。这意味着每个芯片根据自己的状态使用不同的颜色
    • 如何做到这一点,一旦我选择了一个新项目,以前的项目就会失去它们选择的状态和颜色。我只想要一个选定的状态
    猜你喜欢
    • 2017-04-11
    • 2015-11-30
    • 1970-01-01
    • 2015-12-20
    • 2011-06-05
    • 2021-08-06
    • 1970-01-01
    • 2019-09-09
    • 2014-02-12
    相关资源
    最近更新 更多