如何在芯片组中处理多选的芯片?

3

我想处理多个芯片的选择,但是如果我添加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>

请查看此解决方案 https://dev59.com/kFQJ5IYBdhLWcg3wnHPm#53518090 - AskNilesh
我已经尝试过了,但它不起作用...感谢您的支持。 - Himanshu
如之前所建议的,只需定义chip.setOnCheckedChangeListener - Gabriele Mariotti
1个回答

4

我使用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"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接