从ChipGroup获取选定的芯片

时间:2019-04-29 14:46:58

标签: android android-chips

我刚接触Android中的Chips。 当我单击按钮时,我想从ChipGroup中获取选定的Chips。 Made是检查每个Chip并将其添加到Set中的一种工作方式,但是要使其效率更高。不知何故我自己也找不到答案。

当我检查2个筹码并取消选中我检查的第一个筹码时,也会出现错误。

这是我的代码:

<com.google.android.material.chip.ChipGroup
                android:id="@+id/chipGroup"
                android:layout_width="300dp"
                android:layout_height="wrap_content"

                >

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip1"
                    style="@style/Widget.MaterialComponents.Chip.Filter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1"
                    android:backgroundTint="#99FFFFFF"
                    />

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip2"
                    style="@style/Widget.MaterialComponents.Chip.Filter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2"
                    android:backgroundTint="#99FFFFFF"/>


                <com.google.android.material.chip.Chip
                    android:id="@+id/chip3"
                    style="@style/Widget.MaterialComponents.Chip.Filter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3"
                    android:backgroundTint="#99FFFFFF"/>

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip4"
                    style="@style/Widget.MaterialComponents.Chip.Filter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4"
                    android:backgroundTint="#99FFFFFF"/>

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip5"
                    style="@style/Widget.MaterialComponents.Chip.Filter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="5"
                    android:backgroundTint="#99FFFFFF"/>

            </com.google.android.material.chip.ChipGroup>
Set<Integer> chipIds = new HashSet<>();

int chip1Id= 1;
int chip2Id= 2;
int chip3Id= 3;
int chip4Id= 4;
int chip5Id= 5;

chip1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chipIds.add(chip1Id);
                } else {
                    for (int i : chipIds) {
                        if (i == chip1Id) {
                            chipIds.remove(i);
                        }
                    }
                }
            }
        });

        chip2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chipIds.add(chip2Id);
                } else {
                    for (int i : chipIds) {
                        if (i == chip2Id) {
                            chipIds.remove(i);
                        }
                    }
                }
            }
        });

        chip3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chipIds.add(chip3Id);
                } else {
                    for (int i : chipIds) {
                        if (i == chip3Id) {
                            chipIds.remove(i);
                        }
                    }
                }
            }
        });

        chip4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chipIds.add(chip4Id);
                } else {
                    for (int i : chipIds) {
                        if (i == chip4Id) {
                            chipIds.remove(i);
                        }
                    }
                }
            }
        });

        chip5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chipIds.add(chip5Id);
                } else {
                    for (int i : chipIds) {
                        if (i == chip5Id) {
                            chipIds.remove(i);
                        }
                    }
                }
            }
        });

3 个答案:

答案 0 :(得分:1)

ChipGroup没有任何方法可以返回多个选定的筹码。因此,您可以使用传统方式,将所有子对象都循​​环到父对象中,然后检查是否已选中。

 for (index in 0 until mBinding.bookingIncludeCg.childCount) {
      val chip:Chip = mBinding.bookingIncludeCg.getChildAt(index) as Chip
          when(chip.id) {
              R.id.free_cancellation_chip -> hotelFilter.freeCancellation = chip.isChecked
              R.id.free_breakfast_chip -> hotelFilter.freeBreakfast = chip.isChecked
              R.id.free_wifi -> hotelFilter.freeWifi = chip.isChecked
          }
 }

还有XML

<android.support.design.chip.ChipGroup
        android:id="@+id/booking_include_cg"
        android:layout_marginTop="@dimen/spacing_16"
        app:layout_constraintTop_toBottomOf="@+id/booking_include_label"
        app:layout_constraintLeft_toLeftOf="@id/guidelineLeft"
        app:layout_constraintRight_toRightOf="@id/guidelineRight"
        android:layout_width="0dp"
        android:layout_height="wrap_content">

        <android.support.design.chip.Chip
            android:id="@+id/free_cancellation_chip"
            style="@style/HotelFilterChipStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:chipText="@string/free_cancellation" />

        <android.support.design.chip.Chip
            android:id="@+id/free_breakfast_chip"
            style="@style/HotelFilterChipStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:chipText="@string/free_breakfast" />

        <android.support.design.chip.Chip
            android:id="@+id/free_wifi"
            style="@style/HotelFilterChipStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:chipText="@string/free_wifi" />

    </android.support.design.chip.ChipGroup>

答案 1 :(得分:1)

这是我的解决方法:

  1. 为了测试结果,我在XML中添加了一个按钮
<Button
  android:id="@+id/bShow"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@id/chipGroup"
  android:layout_margin="10dp"
  android:text="Show Checked"  
  android:onClick="buttonPressed"/>
  1. Java:仅用于测试,请确保您包括:
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
  1. Java:添加以下代码
showResult = (Button) findViewById(R.id.bShow);
showResult.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v) {
        String msg = "Chips checked are:";
        ChipGroup chg = findViewById(R.id.chipGroup);
        int chipsCount = chg.getChildCount();
        if (chipsCount == 0) {
            msg += " None!!";
        } else {
            int i = 0;
            while (i < chipsCount) {
              Chip chip = (Chip) chg.getChildAt(i);
              if (chip.isChecked() ) {
                msg += chip.getText().toString() + " " ;  
              }
              i++;
            };  
        }
        // show message
        Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
    }
});

答案 2 :(得分:0)

对于那些寻找 Kotlin 的人来说,它看起来要简单得多。此版本也使用Sequence,因此比通常的Java版本更有效:

val chipGroup = // get the ChipGroup view via your favorite inflation mechanism (view bindind, android extensions, etc)
val selectedChips = chipGroup.children
                .filter { (it as Chip).isChecked }
                .map { (it as Chip).text.toString() }