我如何处理芯片组中的多种芯片选择?

时间:2019-04-22 05:46:58

标签: android android-chips

我想处理多个筹码选择,但如果我添加 app:singleSelection =“ true” ,并且如果我这样做,则 Chipgroup.setOnCheckedChangedListener(); 方法有效无法选择多个筹码。我不明白如何从芯片组中选择多个芯片。

MainActivity.java

 private ChipGroup chipGroup;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                chipGroup = findViewById(R.id.chipg);

                for (int i = 0; i < 10; i++) {
                    ChipMaking(String.valueOf(i));
                }

                chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(ChipGroup chipGroup, @IdRes int i) {
                        Chip chip = chipGroup.findViewById(i);
                        Toast.makeText(getApplicationContext(), "Chip is " + chip.getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                });

            }

            public void ChipMaking(String tag) {
                Chip chip = new Chip(this);
                chip.setId(Integer.parseInt(tag));
                chip.setText(tag);
                chip.setTextAppearanceResource(R.style.ChipTextStyle);
                chip.setPaddingRelative(5, 5, 5, 5);
                chip.setElevation(5);
                chip.setCheckable(true);
                chip.setClickable(true);
                chipGroup.addView(chip);
            }

MainActivity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp">

            <com.google.android.material.chip.ChipGroup
                android:id="@+id/chipg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_marginTop="20dp"
                android:foregroundGravity="center">
            </com.google.android.material.chip.ChipGroup>
        </ScrollView>

        <!--app:singleSelection="true"-->
    </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我以编程方式添加带有setOnCheckedChangeListener的新筹码,并使用private int检查所选筹码的数量。

View newChip = LayoutInflater.from(this).inflate(R.layout.chip, null);
((Chip) newChip).setOnCheckedChangeListener(changeListener);
chipGroup.addView(newChip);

private CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            chipNumber = chipNumber + 1;
        } else {
            if (chipNumber > 0) {
                chipNumber = chipNumber - 1;
            }
        }
    }
};

if (chipNumber > 0)可能不是必需的。 似乎是这样,您可以控制选定的芯片。

这也可以使用ChipGroup的singleSelection="false"