ChipGroup单选

adr*_*oir 14 android android-chips material-components material-components-android

我怎样才能强迫一个ChipGroup人像RadioGroup至少一个选定的项目一样?设置setSingleSelection(true)还增加了在a上单击两次时没有选择任何内容的可能性Chip.

Gab*_*tti 76

为了防止所有筹码被取消选择,您可以使用以下方法setSelectionRequired

chipGroup.setSelectionRequired(true)
Run Code Online (Sandbox Code Playgroud)

您还可以使用app:selectionRequired属性在布局中定义它:

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

注意:这至少需要1.2.0版本

  • 这需要获得赞成票。该功能不再需要像其他答案一样进行破解。 (2认同)

adr*_*oir 11

解决方案是预设一个点击的芯片,然后切换芯片的可点击属性:

chipGroup.setOnCheckedChangeListener((chipGroup, id) -> {
    Chip chip = ((Chip) chipGroup.getChildAt(chipGroup.getCheckedChipId()));
    if (chip != null) {
        for (int i = 0; i < chipGroup.getChildCount(); ++i) {
            chipGroup.getChildAt(i).setClickable(true);
        }
        chip.setClickable(false);
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 芯片芯片=chipGroup.findViewById(chipGroup.getCheckedChipId()); (2认同)

Joa*_*Ley 9

有两个步骤可以实现这一目标

步骤1

我们内置了此支持,只需确保添加app:singleSelection="true"到您的中ChipGroup,例如:

XML格式

<com.google.android.material.chip.ChipGroup
            android:id="@+id/group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:singleSelection="true">

        <com.google.android.material.chip.Chip
                android:id="@+id/option_1"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Option 1" />

        <com.google.android.material.chip.Chip
                android:id="@+id/option_2"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Option 2" />
</com.google.android.material.chip.ChipGroup>

Run Code Online (Sandbox Code Playgroud)


// Kotlin
group.isSingleSelection = true

// Java
group.setSingleSelection(true);

Run Code Online (Sandbox Code Playgroud)

第2步

现在要支持类似广播组的功能:


var lastCheckedId = View.NO_ID
chipGroup.setOnCheckedChangeListener { group, checkedId ->
    if(checkedId == View.NO_ID) {
        // User tried to uncheck, make sure to keep the chip checked          
        group.check(lastCheckedId)
        return@setOnCheckedChangeListener
    }
    lastCheckedId = checkedId

    // New selection happened, do your logic here.
    (...)

}

Run Code Online (Sandbox Code Playgroud)

文档

ChipGroup还支持一组芯片的多重排除范围。设置app:singleSelection属性时,检查属于芯片组的一个芯片会取消选中同一组中以前检查过的所有芯片。该行为反映了RadioGroup的行为。


Tod*_*and 6

@adriennoir的答案的简短修改(在Kotlin中)。谢谢您的帮助!请注意,它getChildAt()需要一个索引。

for (i in 0 until group.childCount) {
    val chip = group.getChildAt(i)
    chip.isClickable = chip.id != group.checkedChipId
}
Run Code Online (Sandbox Code Playgroud)

这是我更大的`setOnCheckedChangeListener'的上下文:

intervalChipGroup.setOnCheckedChangeListener { group, checkedId ->

    for (i in 0 until group.childCount) {
        val chip = group.getChildAt(i)
        chip.isClickable = chip.id != group.checkedChipId
    }

    when (checkedId) {
        R.id.intervalWeek -> {
            view.findViewById<Chip>(R.id.intervalWeek).chipStrokeWidth = 1F
            view.findViewById<Chip>(R.id.intervalMonth).chipStrokeWidth = 0F
            view.findViewById<Chip>(R.id.intervalYear).chipStrokeWidth = 0F
            currentIntervalSelected = weekInterval
            populateGraph(weekInterval)
        }
        R.id.intervalMonth -> {
            view.findViewById<Chip>(R.id.intervalWeek).chipStrokeWidth = 0F
            view.findViewById<Chip>(R.id.intervalMonth).chipStrokeWidth = 1F
            view.findViewById<Chip>(R.id.intervalYear).chipStrokeWidth = 0F
            currentIntervalSelected = monthInterval
            populateGraph(monthInterval)

        }
        R.id.intervalYear -> {
            view.findViewById<Chip>(R.id.intervalWeek).chipStrokeWidth = 0F
            view.findViewById<Chip>(R.id.intervalMonth).chipStrokeWidth = 0F
            view.findViewById<Chip>(R.id.intervalYear).chipStrokeWidth = 1F
            currentIntervalSelected = yearInterval
            populateGraph(yearInterval)
        }
    }

}
Run Code Online (Sandbox Code Playgroud)


tok*_*rio 5

大多数答案都很好,对我很有帮助。对 @adriennoir 和 @Todd DeLand 的另一个轻微修改,为了防止取消setSingleSelection(true)选中 ChipGroup 中已检查的芯片,这是我的解决方案:

for (i in 0 until chipGroup.childCount) {
    val chip = chipGroup.getChildAt(i) as Chip
    chip.isCheckable = chip.id != chipGroup.checkedChipId
    chip.isChecked = chip.id == chipGroup.checkedChipId
}
Run Code Online (Sandbox Code Playgroud)

对我来说,我只需要防止相同的选中芯片被取消选中,而不使其不可点击。这样,用户仍然可以单击选中的芯片并看到奇特的涟漪效果,而不会发生任何事情。