Dynamic Radio Button Control

后端 未结 3 1030
臣服心动
臣服心动 2020-12-31 11:40

Code ...{

private void createRadioButton() {

        final RadioButton[] rb = new RadioButton[5];
        for(int i=0; i<5; i++){
            rb[i]  = ne         


        
3条回答
  •  星月不相逢
    2020-12-31 12:34

    You must add the radio buttons to a RadioGroup and then the RadioGroup to the layout

    I miss some information like what is submit, but your code should look like:

    private void createRadioButton() {
        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].setText("Test");
        }
        ll.addView(rg);//you add the whole RadioGroup to the layout
        ll.addView(submit); 
        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                for(int i = 0; i < 5; i++) { 
                    rg.removeView(rb[i]);//now the RadioButtons are in the RadioGroup
                }  
                ll.removeView(submit);
                Questions();
            }
        });   
    }
    

提交回复
热议问题