如何从ChipGroup获得选定的芯片?

时间:2019-10-03 18:06:02

标签: java android material-components-android material-components android-chips

我在互联网上进行了很多搜索,但找不到确切的解决方案。这是我上次尝试的链接。 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());
                }
            }
        }
    });
}

这是输出 enter image description here

build.gradle(Module.app)详细信息 enter image description here

5 个答案:

答案 0 :(得分:3)

当前没有直接的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..... 
      }
    }

编辑: 在1.2.0版中,您可以使用方法chipGroup.getCheckedChipIds()

答案 1 :(得分:2)

在芯片组中获取所选芯片的文本

要从芯片组中获取所选芯片的文本,您可以在 Kotlin 中使用这一行:

val selectedChipText = parentChipGroup.findViewById<Chip>(parentChipGroup.checkedChipId).text.toString()

答案 2 :(得分:0)

我使用下面提到的方法来检测chipGroup中选定的芯片。

for (chipTitle in stringList) {
    val chip = Chip(chipGroup.context)
    chip.text = chipTitle
    chip.tag = chipTitle
    chip.isClickable = true
    chip.isCheckable = true
    chip.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked){
            Log.e(TAG, chipTitle)
        }
    }
    chipGroup.addView(chip)
}
chipGroup.isSingleSelection = true

答案 3 :(得分:0)

我在Kotlin中使用的具有数据绑定的解决方案

mBinding?.chipGroup?.children
            ?.toList()
            ?.filter { (it as Chip).isChecked }
            ?.forEach { //here are your selected chips as 'it' }

这就是我获得标题的方式

mBinding?.chipGroup?.children
            ?.toList()
            ?.filter { (it as Chip).isChecked }
            ?.joinToString(", ") { (it as Chip).text }

答案 4 :(得分:-1)

现在使用材料芯片是非常容易的任务。 这是从对话框中获取芯片文本的示例

       val builder = AlertDialog.Builder(requireContext())
       builder.setTitle("Filter")
       val mView = FilterOptionLayoutBinding.inflate(layoutInflater)
       builder.setView(mView.root)

       builder.setPositiveButton(android.R.string.ok) { d, p ->
           mView.chipGroup.checkedChipIds.forEach {
               val chip = mView.root.findViewById<Chip>(it).text.toString()
               checkedChipsText.add(chip)
           }
           d.dismiss()
       }
       builder.setNegativeButton(android.R.string.cancel) { d, _ ->
           d.cancel()
       }
       builder.create()
       builder.show()

现在筹码保存所有筹码的文本