创建动态单选按钮并仅检查一个单选按钮并取消选中其余按钮

时间:2013-11-23 07:41:33

标签: android

我想创建一个包含type_of_service,name,ph.no,...等的表单。在服务类型中,使用单选按钮指定类型。从asp webservice动态地提交..它可能是1,2或10 .. 我必须从服务类型中只选择一个选项...然后在选择选项并填写表单并单击提交按钮后,我必须将所选单选按钮的值传递给支付网关(paypal) ......

我有动态创建单选按钮并选择其中一个...并取消选择剩余的单选按钮... 以及如何检查选择了哪个单选按钮,以便传递相应的值..

plz help ...

1 个答案:

答案 0 :(得分:1)

试试这个..

LinearLayout layout_name = (LinearLayout) findViewById(R.id.layout_name);
final RadioButton[] rb = new RadioButton[5];
        RadioGroup rg = new RadioGroup(this); //create the RadioGroup
        rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
        for(int i=0; i<5; i++){
            rb[i]  = new RadioButton(this);
            rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
            rb[i].setId(i);//Setting id for the RadioButton
            rb[i].setText("Test");
        }
        rg.check(3);//Checking pirticular RadioButton with id
        // 3 is that RadioButton Id name
        layout_name.addView(rg);//you add the whole RadioGroup to the layout

修改

 int selected = rg.getCheckedRadioButtonId();
 String type =rb[selected].getText().toString(); 
 Log.d("type", type);