【问题标题】:How i handle multiple selection of chips in chipgroup?我如何处理芯片组中的多个芯片选择?
【发布时间】:2019-09-11 08:51:38
【问题描述】:

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

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>

【问题讨论】:

  • 检查这个解决方案stackoverflow.com/a/53518090/7666442
  • 我已经尝试过了,但它不起作用......感谢您的支持。
  • 如前所述,只需定义chip.setOnCheckedChangeListener

标签: android android-chips


【解决方案1】:

我以编程方式添加带有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 &gt; 0) 可能不是必需的。 好像这样,你可以控制选中的筹码。

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

【讨论】:

    猜你喜欢
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    相关资源
    最近更新 更多