开发者

adding buttons and radiobuttons dynamically in android

开发者 https://www.devze.com 2023-03-06 09:06 出处:网络
In my project I want to add buttons and radio buttons dynamically. They should be placed side by side. For instance,开发者_开发百科 if I had 10 buttons and 10 radiobuttons, I should get 2 buutons and

In my project I want to add buttons and radio buttons dynamically. They should be placed side by side. For instance,开发者_开发百科 if I had 10 buttons and 10 radiobuttons, I should get 2 buutons and 2 radiobuttons in each row. How can this be done?


for example if you want to add 7 buttons and radiobuttons to your LinearLayout you can use something like this

    private void createRadioButton() {
        final RadioButton[] radioButton = new RadioButton[7];
        RadioGroup radioGroup = new RadioGroup(this);
        radioGroup.setOrientation(RadioGroup.VERTICAL);
        for(int i = 0; i < 7; ++i) {
            radioButton[i]  = new RadioButton(this);
            Button buttonView = new Button(this);
            buttonView.setText("Button " + i);
            radioGroup.addView(radioButton[i]);
            radioGroup.addView(buttonView);
            radioButton[i].setText("Test" + i);
        }
        line.addView(radioGroup);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消