[英]Android: Material Design Chip - show close icon only when selected
I'm using Material Chip and I want to show closeIcon
only when the Chip
is selected.我正在使用 Material Chip,我只想在选择Chip
时显示closeIcon
。
How can I achieve this behavior?我怎样才能实现这种行为? I can do some binding etc, but I would prefer some simplier solution.我可以做一些绑定等,但我更喜欢一些更简单的解决方案。
<com.google.android.material.chip.ChipGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipSpacing="8dp"
app:singleLine="true"
app:singleSelection="false">
<com.google.android.material.chip.Chip
android:id="@+id/chip_1"
style="@style/MyChipStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:closeIcon="@drawable/ic_cross_white_24dp"
app:closeIconEnabled="true"
app:checkedIconEnabled="false"
android:checkable="true"
android:text="Example text" />
</com.google.android.material.chip.ChipGroup>
You need to add code for checking that chip is selected or not.您需要添加代码以检查是否选择了芯片。
Try below code to check:试试下面的代码来检查:
private void chipCloseBtnVisi(){
if(chip_1.isChecked()){
// If Chip is selected(checked) then show close icon
chip_1.setCloseIconVisible(true);
}else{
// If Chip is not selected(checked) then hide close icon
chip_1.setCloseIconVisible(false);
}
}
To change check state, need to add setOnCheckedChangeListener
to chip要更改检查 state,需要将setOnCheckedChangeListener
添加到芯片
chip_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
chipCloseBtnVisi();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.