【问题标题】:android Get Dynamically checkbox valueandroid 动态获取复选框值
【发布时间】:2021-07-01 10:35:03
【问题描述】:

您好,我是一名学生,我的项目是通过 android studio 制作应用程序。

我只是一个初学者,所以一切都很困难:(

这是我的问题。

我根据当前动态创建了复选框。

如果用户单击 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));

   }

另一个错误

String[] membersA = {"all", "ji", "ha", "bo", "re", "co", "zo", "pi"};
                    arrayMember = new ArrayList<>();

for (int i = 0; i < membersBts.length; i++) {
                        CheckBox chA = new CheckBox(getApplicationContext());
                        chA .setText(members[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);
                                    arrayMember.add(index, buttonView.getText().toString());
                                }

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

                            }
                        });

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

错误日志

    java.lang.IndexOutOfBoundsException: Index: 5, Size: 2

【问题讨论】:

    标签: java android dynamic


    【解决方案1】:

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

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

    私有字符串[] finalMemberTag = 新字符串[16]

    其次,我认为您最好使用字符串集以避免重复,或者,如果您需要有关特定索引的信息,则使用数组列表。

    搭配套装:

     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(buttonText); //this will add the text
             } else {
                 // other stuff ...
                 finalMembersTag.remove(buttonText); //this will remove the element if found, or if you want an empty string you can do finalMembersTag.add(index, "");
             }
         }
    

    【讨论】:

    • 您好,非常感谢您的友好回答。我试了一下,它工作了一段时间。(单击复选框几次)当我多次单击复选框时,我收到类似 java.lang.IndexOutOfBoundsException 的错误你能告诉我我该如何解决这个问题?我搜索了一下,但我不知道为什么我的索引大于大小。我更新了我的答案。
    • 是的,你必须小心使用索引删除,数组会更短,我改变了答案,所以你使用finalMembersTag.remove(buttonText)。此外,在不存在的索引处添加一些东西会给你同样的错误,所以最好在你的特定情况下使用finalMembersTag.add(buttonText)而不使用索引
    猜你喜欢
    • 2019-08-05
    • 2014-08-06
    • 1970-01-01
    • 2017-05-07
    • 2012-06-07
    • 2018-02-17
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多