android中radioGroup动态添加radioButton

radioGroup这个控件相信大家都有已经早已熟练了吧,这个控件也是很好用的,但是现在的需求是,根据网络获取的数据,来生成加载radioButton,这样的话,我们只能动态加载radioButton,因此我们今天就来介绍一下radioGroup动态添加radioButton:

1.首先看我们的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.radiogroupdynamicloadingdemo.MainActivity">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</ScrollView>

2.我们在写一个名为radio_bg的selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/blue"/>
    <item android:state_checked="false" android:drawable="@drawable/white"/>
</selector>

3.然后在MainActivity中使用:


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        for (int i = 0; i < 10; i++) {
            RadioButton radioButton = new RadioButton(this);
            RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
            //设置RadioButton边距 (int left, int top, int right, int bottom)
            lp.setMargins(15,0,0,0);
            //设置RadioButton背景
            //radioButton.setBackgroundResource(R.drawable.xx);
            //设置RadioButton的样式
            radioButton.setButtonDrawable(R.drawable.radio_bg);
            //设置文字距离四周的距离
            radioButton.setPadding(80, 0, 0, 0);
            //设置文字
            radioButton.setText("this is radioButton  " + i);
            final int finalI = i;
            //设置radioButton的点击事件
            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "this is radioButton  " + finalI, Toast.LENGTH_SHORT).show();
                }
            });
            //将radioButton添加到radioGroup中
            radioGroup.addView(radioButton);
        }
    }
}

4.效果图如下:
这里写图片描述

5.demo地址:

http://download.csdn.net/detail/afanbaby/9791444

本人菜鸟一个,有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步!

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 应用程序,我们很有可能需要在某些情况下动态添加 RadioGroup。例如,在某些特殊的表单或设置页面,我们需要在运行时动态添加 RadioGroup。 首先,我们需要在 XML 布局文件添加 RadioGroup 的容器视图,并为其分配一个 ID。然后,在 Java 代码,我们可以使用 findViewById() 方法来引用 RadioGroup 容器,并创建一个新的 RadioButton 实例来添加RadioGroup 。为了使RadioButtonRadioGroup互斥,我们还需要通过 setOnClickListener() 方法指定一个单选按钮选择监听器,以确保鼠标单击不同的单选按钮时只选择一个项目。 以下是动态添加 RadioGroup 的示例代码: ``` // 获取 RadioGroup 的视图 RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup); // 创建 RadioButton 实例 RadioButton radioButton1 = new RadioButton(this); radioButton1.setText("Radio Button 1"); RadioButton radioButton2 = new RadioButton(this); radioButton2.setText("Radio Button 2"); RadioButton radioButton3 = new RadioButton(this); radioButton3.setText("Radio Button 3"); // 将 RadioButton 添加RadioGroup radioGroup.addView(radioButton1); radioGroup.addView(radioButton2); radioGroup.addView(radioButton3); // 添加单选按钮选择监听器 radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { // 当选择一个单选按钮时,只对选择的单选按钮执行操作 } }); ``` 实际上,RadioGroup添加操作非常灵活,可以使用不同的方法根据自己的需求添加单选按钮。此外,为了方便起见,我们可以通过 findViewById() 方法和 setVisibility() 方法在运行时动态更改 RadioGroup 的可见性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值