【问题标题】:Cant get the values from dynamically added check box无法从动态添加的复选框中获取值
【发布时间】:2014-05-16 07:45:15
【问题描述】:

点击按钮时我添加了一个复选框,最后需要通过点击提交按钮来获取所有选中的复选框值,

这是我的代码

mIncrementButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

                params.setMargins(20, 0, 0, 0);

                CheckBox checkbox = new CheckBox(MainActivity.this);

                checkbox.setLayoutParams(params);
                mAllText.add(checkbox);
                checkbox.setText("Checkbox" + j);

                checkboxlayout.addView(checkbox);
                j++;

                Log.d("j", "--->" + j);

            }
        });

        mGetTheVlue.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.d("Inside Button", "Inside Burron" + mAllText.size());

                for (int i = 0; i < mAllText.size(); i++) {
                    CheckBox check = mAllText.get(i);
                    if (check.isChecked()) {
                        AllCheckbox.add(check.getText().toString());
                    }

                }

            }
        });

现在我需要检查是否所有复选框都被选中并需要获取值, 不知道这个问题是否太老了。但我无法从谷歌获得任何解决方案,请帮助获得解决方案。

提前致谢。

【问题讨论】:

  • 什么是mAllText? CheckBox 的数组列表。尝试为所有复选框设置 id。
  • mAllText 是一个动态添加复选框的arraylist,设置id后如何获取值?
  • 将您的 j 值设置为您的复选框的 id。复选框.setId(j)。并使用oncheckChangedListener检查复选框是否被选中。
  • 得到了解决方案,无论如何感谢您的出色回应

标签: android dynamic checkbox


【解决方案1】:

此外,您不需要将所有 CheckBox 存储在 Collection 中。您可以从布局中获取子视图并检查它们中的每一个是否为 CheckBox。

    mGetTheVlue.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.d("Inside Button", "Inside Burron" + checkboxlayout.getChildCount());

            for(int i=0; i<checkboxlayout.getChildCount(); i++) {
                View nextChild = checkboxlayout.getChildAt(i);

                if(nextChild instanceOf CheckBox)
                {
                    CheckBox check = (CheckBox) nextChild;
                    if (check.isChecked()) {
                                AllCheckbox.add(check.getText().toString());
                    }
                }

        }

        }
    });
猜你喜欢
  • 2021-06-17
  • 2013-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
相关资源
最近更新 更多