动态添加试题选项按钮 radioButton(一)

最近在做WebView加载试题的功能,但是选项按钮如果放的WebView中,点击时反应很慢。于是把选项用原生的RadioButton,而试题题目和答案放在WebView中。但是选项的个数不确定,所以需要动态添加按钮,一个RadioGroup里面存放若干Radio(这些都是从数据库获取来的数据),然后每次动态生成Radio

以下是动态添加按钮的代码:——————————————————

private LinearLayout layout; //布局 , 可以在xml布局中获得

 private RadioGroup group ; //点选按钮组

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        

        layout = new LinearLayout(this); //实例化布局对象

        

        group = new RadioGroup(this); //实例化单选按钮组

        

        //添加单选按钮

        for(int i = 0 ; i < 5 ; i++){

         RadioButton radio = new RadioButton(this);

         radio.setText("radio" + i);

         group.addView(radio);

        }

        

        //将单选按钮组添加到布局中

        layout.addView(group);

        

        this.setContentView(layout);

    } 

如果题目只能选择一次,则当点击选项事件发生后,其他radiobutton要禁用,代码:

 rgSelect.setEnabled(false);//rgSelect是radioGroup
                                     for(int i = 0; i < rgSelect.getChildCount(); i++){
                                            ((RadioButton)rgSelect.getChildAt(i)).setEnabled(false);
                                        }

 【转载】http://blog.sina.com.cn/s/blog_6dc41baf01019214.html

原文地址:https://www.cnblogs.com/lucky-star-star/p/3822566.html