3

複数のチップの選択を処理したいのですが、Chipgroup.setOnCheckedChangedListener(); app:singleSelection = "true"を追加するとメソッドが機能し、そうすると複数のチップを選択できなくなります。chipgroup から複数のチップを選択する方法がわかりません。

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

1 に答える 1