我在互联网上搜索了很多,但找不到确切的解决方案。这是我上次尝试的链接。
Get selected Chips from a ChipGroup

单击按钮后,我想从芯片组中选择芯片。

此函数包含在RecyclerView中显示名称的信息

private void getNames() {
    List<String> names = Arrays.asList(getResources().getStringArray(R.array.names));
    int count = 0;
    for ( String name : names){
        list.add(new Names(name));
        count++;
    }
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    namesAdapter = new NamesAdapter(MainActivity.this, list);
    recyclerView.setAdapter(namesAdapter);
}

当单击RecyclerView项时,一个芯片被添加到ChipGroup中,这是函数
public void onItemSelected(Names name) {
    Chip chip = new Chip(this);
    chip.setText(name.getName());
    chip.setCloseIconVisible(true);
    chip.setCheckable(false);
    chip.setClickable(false);
    chip.setOnCloseIconClickListener(this);
    chipGroup.addView(chip);
    chipGroup.setVisibility(View.VISIBLE);
}

This is the image of displaying chips in ChipGroup

从ChipGroup中获取值的函数
public void getChipGroupValues(){
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ChipGroup chipGroup = findViewById(R.id.chipGroup);
            for (int i=0; i<chipGroup.getChildCount();i++){
                Chip chip = (Chip)chipGroup.getChildAt(i);
                Log.i("outside if ", i+ " chip = " + chip.getText().toString());
                if (chip.isChecked()){
                    Log.i("inside if ", i+ " chip = " + chip.getText().toString());
                    textView.setText(chip.getText().toString());
                }
            }
        }
    });
}

这是输出
java - 如何从ChipGroup获得选定的芯片?-LMLPHP

build.gradle(Module.app)详细信息
java - 如何从ChipGroup获得选定的芯片?-LMLPHP

最佳答案

1.2.0 版本开始,您可以使用方法chipGroup.getCheckedChipIds()

List<Integer> ids = chipGroup.getCheckedChipIds();
for (Integer id:ids){
      Chip chip = chipGroup.findViewById(id);
      //....
}

含1.1.0的旧答案:

当前没有直接的API来获取选定的芯片。
但是,您可以遍历ChipGroup的子级并检查chip.isChecked()
   ChipGroup chipGroup = findViewById(R.id.....);
   for (int i=0; i<chipGroup.getChildCount();i++){
      Chip chip = (Chip)chipGroup.getChildAt(i);
      if (chip.isChecked()){
         //this chip is selected.....
      }
    }

09-05 02:18