簡體   English   中英

Android:材料設計芯片 - 僅在選擇時顯示關閉圖標

[英]Android: Material Design Chip - show close icon only when selected

我正在使用 Material Chip,我只想在選擇Chip時顯示closeIcon

我怎樣才能實現這種行為? 我可以做一些綁定等,但我更喜歡一些更簡單的解決方案。

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

您需要添加代碼以檢查是否選擇了芯片。

試試下面的代碼來檢查:

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

要更改檢查 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.

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