【问题标题】:How to get values from dynamic radiogroup android?如何从动态radiogroup android中获取值?
【发布时间】:2015-06-03 18:12:11
【问题描述】:

您好,我使用单选按钮创建了动态单选组。现在我想从动态单选组中获取值。下面是我的代码

final RadioGroup rg = new RadioGroup(this);
            rg.setId(questionNo);

            RadioButton[] rb = new RadioButton[answers.length()];

            for (int i = 0; i < answers.length(); i++) {
                JSONObject answerObject = answers.getJSONObject(i);
                rb[i] = new RadioButton(this);
                rb[i].setText(answerObject.getString("AnswerValue"));
                rb[i].setId(answerObject.getInt("AnswerId"));

                rg.addView(rb[i]);
            }



rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup arg0, int arg1) {
                        int selectedId = rg.getCheckedRadioButtonId();
                        Log.i("ID", String.valueOf(selectedId));

                    }
                });

按钮点击事件

submit_button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(rg.getCheckedRadioButtonId()!=-1){
                int id= rg.getCheckedRadioButtonId();
                View radioButton = rg.findViewById(id);
                int radioId = rg.indexOfChild(radioButton);
                RadioButton btn = (RadioButton) rg.getChildAt(radioId);
                String selection = (String) btn.getText();
                Log.i("selection", selection);

            }
            }
        });

我只得到广播组的最后一个索引。

【问题讨论】:

  • @codeMagic 如何从 radiogroup 中获取所有选定的值?
  • @codeMagic 嗨,我只得到最后一个值
  • 因为它们都在一个RadioGroup 中,所以您一次只能选择一个。您需要将它们放在不同的组中或使用复选框

标签: android radio-group


【解决方案1】:

你可以通过这种方式实现这个目标

radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
     void onCheckedChanged(RadioGroup rg, int checkedId) {
          for(int i=0; i<rg.getChildCount(); i++) {
               RadioButton btn = (RadioButton) rg.getChildAt(i);
               if(btn.getId() == checkedId) {
                    String text = btn.getText();
                    // do something with text
                    return;
               }
          }
     }
});

【讨论】:

  • 您应该考虑在您的代码中添加一个解释,说明为什么您的代码应该满足 OP 的需求。
【解决方案2】:

我知道为时已晚,但这可能对其他人有所帮助

在创建动态单选按钮组时,创建父布局(线性布局或相对布局)

所有单选组将是该布局的子项,所有单选按钮将是特定单选组的子项。

创建动态单选组编号

  • 在父布局中创建所有动态单选组
  • 将所有单选按钮添加到组中
  • 将所有单选组添加到父布局

        public RadioButton[] rb;
        public RadioGroup grp;
        public LinearLayout layoutmain;             //Parent layout
    
    for(int i=0;i<qus.size();i++)                   //No of Questions
    {
    
        rb = new RadioButton[size];
        grp = new RadioGroup(this);
    
        grp.setId(a+qus.get(i).qusetionId);
    
        for(int j=0;j<qus.get(i).choices.size();j++)    //No of Radio button in radio group
        {
    
            rb[j] = new RadioButton(this);
            rb[j].setText(qus.get(i).choices.get(j).getChoiceText());
            grp.addView(rb[j]);                         // adding button to group
    
        }
        layoutmain.addView(grp);                        // adding group to layout
    }
    

检索所有选定的按钮任务

  • 从父级,布局获取所有子级
  • 使用子实例查找所有无线电组
  • 为该无线电组创建一个对象
  • 使用该对象获取选中的单选按钮

    Button bt = (Button) findViewById(R.id.button);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    
            for(int i=0;i<layoutmain.getChildCount();i++)  //From the parent layout (Linear Layout) get the child
            {
                View child = layoutmain.getChildAt(i);  
    
                if(child instanceof RadioGroup)     //Check weather its RadioGroup using its INSTANCE
                {
                    RadioGroup rg = (RadioGroup) child; //create a RadioButton for each group
                    int selectedId = rg.getCheckedRadioButtonId(); // get the selected button
    
                        RadioButton radiochecked = (RadioButton) findViewById(selectedId);
                        Log.e("Radio",radiochecked.getText().toString());   //from the button get the selected text
    
                }
            }
        } });
    

【讨论】:

    猜你喜欢
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    相关资源
    最近更新 更多