【问题标题】:How to get selected chips from ChipGroup?如何从 ChipGroup 获得精选的芯片?
【发布时间】:2020-02-02 02:20:21
【问题描述】:

我在互联网上搜索了很多,但找不到确切的解决方案。这是我上次从 SO 尝试的链接。 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 项目时,将一个芯片添加到芯片组中,这是功能

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());
                }
            }
        }
    });
}

这是输出

build.gradle(Module.app) 详情

【问题讨论】:

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


【解决方案1】:

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

       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()

现在芯片保存所有芯片的文本

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 或者如果你已经填充了标签变量,你可以使用它,比如:siteTypeId = registerCardChipGroupSiteTypes .findViewById&lt;Chip&gt;(registerCardChipGroupSiteTypes.checkedChipId).tag.toString().toLong()
    【解决方案3】:

    我在 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.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..... 
            }
          }
      

      【讨论】:

      • 有点接近,这工作正常,但在“if”语句之外,我得到的所有值都不在“if”条件下。该声明的目的是什么?
      • @msayubi76 你的问题是我想在点击按钮时从芯片组中获取选定的芯片。在您的 OnClickListener 中,您必须遍历 ChipGroup 并仅获取选定的值。 if 语句是了解每个 chip 是否为 checked 的唯一方法
      • 但在这种情况下,控制不在 if 语句中。我在 if 语句之外获取值
      • @msayubi76 您使用的是哪个版本的材料组件库?我刚刚尝试了代码,它可以工作。
      • 我在依赖项“com.google.android.material:material:1.0.0”中添加了这个
      【解决方案5】:

      我使用下面提到的方法来检测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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-08
        • 1970-01-01
        • 2022-07-06
        • 1970-01-01
        • 2019-09-14
        • 1970-01-01
        • 2019-11-29
        • 1970-01-01
        相关资源
        最近更新 更多