当前位置: 首页 > 知识库问答 >
问题:

android动态获取复选框值

耿永寿
2023-03-14

你好,我是一个学生,我的项目是制作一个应用程序的android工作室。

我只是个初学者,所以每件事都很难:(

这是我的问题。

我动态创建的复选框取决于当前。

如果用户单击btnEx按钮,我想获得finalMemberTag。

但是在这段代码中,当我单击btnEx时,finalMemberTag为空。

如果用户单击复选框,我希望将复选框的文本添加到FinalMemberTag中。并且复选框未选中,则将复选框的文本移至FinalMemberTag。

我怎么能做这些?(我已经把可抽屉的东西摆好了。)

请帮帮我...

 private String[] finalMemberTag;

  Button btnEx;

  btnEx.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendData();
        }
    });


Switch(current){

  case "A":
                    String[] memberA = {"all", "ab", "cd", "ef", "g", "hu", "hi", "dd"};
                    
                    for (int i = 0; i < 8; i++) {
                        CheckBox chA = new CheckBox(getApplicationContext());
                        chA.setText(membersA[i]);
                        chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
                        chA.setTextColor(Color.parseColor("#8D8D8D"));
                        cHA.setButtonDrawable(android.R.color.transparent);
                        chA.setBackground(inactiveCb);
                        int index = i;
                        chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
                                    chA.setBackground(activeCb);
                                    chA.setTextColor(startextColor);
                                    finalMemberTag[index] = buttonView.getText().toString();
                                    
                                }

                                if(!isChecked) {
                                    chA.setBackground(inactiveCb);
                                    chA.setTextColor(Color.parseColor("#8D8D8D")); }
                                    
                            }
                        });


                        chA.setPadding(36, 24, 36, 24);
                        chA.setLayoutParams(params);
                        placeInputMember.addView(chA);
                    }
                    break;

        case "B":
                    String[] memberB = {"all", "abc", "Dcd", "e3f", "DSg", "DGu", "hE", "dV"};
                    
                    for (int i = 0; i < 8; i++) {
                        CheckBox chB = new CheckBox(getApplicationContext());
                        chB.setText(memberB[i]);
                        chB.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
                        chB.setTextColor(Color.parseColor("#8D8D8D"));
                        chB.setButtonDrawable(android.R.color.transparent);
                        chB.setBackground(inactiveCb);
                        int index = i;
                        chB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
                                    chB.setBackground(activeCb);
                                    chB.setTextColor(startextColor);
                                    finalMemberTag[index] = buttonView.getText().toString();
                                   
                                }

                                if(!isChecked) {
                                    chB.setBackground(inactiveCb);
                                    chB.setTextColor(Color.parseColor("#8D8D8D")); }
                                    
                            }
                        });


                        chB.setPadding(36, 24, 36, 24);
                        chB.setLayoutParams(params);
                        placeInputMember.addView(chB);
                    }
                    break;

  }    

    public void sendData(){
 Log.d("##", String.valueOf(finalMemberTag));

   }

共有1个答案

云星波
2023-03-14

好的,您的代码有一些问题:

首先,finalMemberTag始终为空,因为您从未初始化它。如果您希望使用字符串数组,并且希望将文本存储在复选框上,那么我们应该假设您最多将选中所有复选框(16)。这意味着您希望将数组初始化为:

专用字符串[]finalMemberTag=新字符串[16]

其次,我认为最好使用string集来避免重复,或者,如果需要特定索引的信息,使用ArrayList。

与设置:

 private Set<String> finalMembersTag  =  new HashSet<>(16);
 //onCreate...other stuff...for loop
 //...setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         final String buttonText = buttonView.getText().toString();
         if (isChecked) {
             // other stuff ...
             finalMembersTag.add(buttonText);
         } else {
             // other stuff ...
             finalMembersTag.remove(buttonText);
         }
     }

使用ArrayList:

 private List<String> finalMembersTag  =  new ArrayList<>(16);
 //onCreate...other stuff...for loop
  int index = i;
  chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         final String buttonText = buttonView.getText().toString();
         if (isChecked) {
             // other stuff ...
             finalMembersTag.add(index, buttonText); //this will add the text to the required index
         } else {
             // other stuff ...
             finalMembersTag.remove(index); //this will remove the element  at said index, or if you want an empty you can do finalMembersTag.add(index, "");
         }
     }
 类似资料:
  • 问题内容: 所以我得到的代码看起来像这样: 我只需要Javascript来获取当前选中的任何复选框的值。 编辑 :要添加,将只有一个复选框。 问题答案: 对于现代浏览器 : 通过使用: 没有_以下内容的 _纯javascript :

  • 所以我有这样的代码: 我只需要Javascript来获取当前选中的任何复选框的值。 编辑:要添加,只有一个复选框。

  • 问题内容: 我是php的新手,我想根据从MySQL获取的结果动态创建复选框。如果我在employee表中有10条记录,那么它必须创建10个以员工姓名作为value的复选框。我看过几本教程来制作数组复选框等,但无法解决问题。请那里的任何人帮助!!! 问题答案: 试试看: 上面看到的示例依赖于两点才能真正正常运行: 您正在使用MySQL 您的SQL查询必须检索员工的姓名(以便您可以在循环中使用它们

  • 问题内容: 我想获取Flask中复选框的值。我读过一篇类似的文章,并尝试使用和的输出,因为这是我使用的列表,但看来我做错了。这是获取输出的正确方法还是有更好的方法? 问题答案: 你并不需要使用,只是如果只有一个给定名称的输入,尽管它不应该的事。你显示的内容确实有效。这是一个简单的可运行示例: 提交带有两个复选框的表单,然后在终端中打印。请注意,html表单的方法是,post因此数据将位于中。 在某

  • 问题内容: 如何在jQuery中获得复选框的值? 问题答案: 要获取Value属性的值,您可以执行以下操作: 或者,如果您为其设置了或,则可以: 但是,无论是否选中它都会返回 相同的 值,这可能会造成混淆,因为它与提交的表单行为不同。 要检查是否已检查,请执行以下操作:

  • 我有一个复选框,我想说这个复选框:如果(例如)“Einzelberatung Bu”被选中,并且你想选择另一个具有“Einzelberatung…”术语的字段,那么“Komplettes Finanzkonzept”字段应该自动被选中,其他两个字段都应该被取消选中。 正如您所看到的,我是一个绝对的JavaScript初学者。所以希望有人能帮我解决我贴在下面的代码,并告诉我我做错了什么。 谢谢你抽出