动态单选按钮控件 [英] Dynamic Radio Button Control

查看:111
本文介绍了动态单选按钮控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code ... {

Code ...{

private void createRadioButton() {

        final RadioButton[] rb = new RadioButton[5];
        for(int i=0; i<5; i++){
            rb[i]  = new RadioButton(this);
            ll.addView(rb[i]); 
            rb[i].setText("Test");
         }
         ll.addView(submit); 
          submit.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                for(int i = 0; i < 5; i++) { 
                    ll.removeView(rb[i]); 
                }  
                ll.removeView(submit);
                Questions();
         }});   
    }

我遇到的问题是,单选按钮出现,用户可以选择任何一个。作为一个初学者我敢肯定,我没有设置单选按钮正确。用户可以选择所有的五个按钮,然后一旦选定他们不能取消选中它们要么。用户应该只能够选择从五个一个选项...我怎样才能让这个possable?

The issue i am having is that the radio buttons appear and the user can select any one. Being a beginner im sure i am not setting the radio buttons up correctly. The user is able to select all five buttons and then once selected they cant uncheck them either. The user should only be able to select one option from the five...how can i make this possable?

推荐答案

您必须添加单选按钮到的 RadioGroup中然后RadioGroup中的布局

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

我想喜欢什么是提交,但你的code应该是这样一些信息:

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();
        }
    });   
}

这篇关于动态单选按钮控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆