如何从ChipGroup获得选定的芯片? [英] How to get selected chips from ChipGroup?

查看:175
本文介绍了如何从ChipGroup获得选定的芯片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上进行了大量搜索,但找不到确切的解决方案.这是我上次尝试的链接.

build.gradle(Module.app)详细信息

解决方案

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

  List< Integer>ids = chipGroup.getCheckedChipIds();代表(整数ID:ID){芯片芯片= chipGroup.findViewById(id);//..} 

1.1.0版的旧答案:

当前没有直接的API来获取选定的芯片.
但是,您可以遍历 ChipGroup 的子代并检查 chip.isChecked().

  ChipGroup chipGroup = findViewById(R.id .....);对于(int i = 0; i< chipGroup.getChildCount(); i ++){芯片芯片=(Chip)chipGroup.getChildAt(i);如果(chip.isChecked()){//已选择此芯片.....}} 

I search a lot on internet but couldn't find the exact solution. Here is the link that i last tried from SO. Get selected Chips from a ChipGroup

I want to get selected chips from chip group when a button is clicked.

This function contain information of displaying names in 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);
}

When click on RecyclerView item one chip is added into the ChipGroup here is the function

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

Function that is getting values from 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());
                }
            }
        }
    });
}

This is output

build.gradle(Module.app) detail

解决方案

Starting with the version 1.2.0 you can use the method chipGroup.getCheckedChipIds()

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

OLD ANSWER with 1.1.0:

currently there isn't a direct API to get the selected chips.
However you can iterate through the children of the ChipGroup and check 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..... 
      }
    }

这篇关于如何从ChipGroup获得选定的芯片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆