RadioGroup动态添加RadioButton

RadioGroup 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(SizeUtils.dp2px(10), SizeUtils.dp2px(10), SizeUtils.dp2px(10), SizeUtils.dp2px(10));
            //设置RadioButton背景
            radioButton.setBackgroundResource(R.drawable.radiobutton_background);
radioButton.setTextColor(getResources().getColorStateList(R.drawable.radiobutton_textcolor));
            radioButton.setButtonDrawable(null);
            //设置文字距离四周的距离
            radioButton.setPadding(SizeUtils.dp2px(25), SizeUtils.dp2px(15), SizeUtils.dp2px(25), SizeUtils.dp2px(15));
            radioButton.setTextSize(SizeUtils.sp2px(28));
            radioButton.setId(i);
            radioButton.setChecked(i == 0);
            //设置文字
            radioButton.setText(i + " #罐");
            final int finalI = i;
            //设置radioButton的点击事件
            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(TankWarningSettingActy.this, "this is radioButton  " + finalI + "    = " + v.getId(), Toast.LENGTH_SHORT).show();
                }
            });
            //将radioButton添加到radioGroup中
            radioGroup.addView(radioButton, lp);
        }
<HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:scrollbars="none">
        <RadioGroup
            android:id="@+id/radioGroup"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <!--            <RadioButton-->
            <!--                android:id="@+id/tank_warning_setting_1"-->
            <!--                android:layout_width="wrap_content"-->
            <!--                android:layout_height="wrap_content"-->
            <!--                android:layout_margin="@dimen/dp_10"-->
            <!--                android:background="@drawable/radiobutton_background"-->
            <!--                android:button="@null"-->
            <!--                android:checked="true"-->
            <!--                android:padding="@dimen/dp_15"-->
            <!--                android:text="选项1"-->
            <!--                android:textColor="@drawable/radiobutton_textcolor"-->
            <!--                android:textSize="@dimen/sp_28" />-->

            <!--            <RadioButton-->
            <!--                android:id="@+id/tank_warning_setting_2"-->
            <!--                android:layout_width="wrap_content"-->
            <!--                android:layout_height="wrap_content"-->
            <!--                android:layout_margin="@dimen/dp_10"-->
            <!--                android:background="@drawable/radiobutton_background"-->
            <!--                android:button="@null"-->
            <!--                android:padding="@dimen/dp_15"-->
            <!--                android:text="选项1"-->
            <!--                android:textColor="@drawable/radiobutton_textcolor"-->
            <!--                android:textSize="@dimen/sp_28" />-->

        </RadioGroup>
    </HorizontalScrollView>
radiobutton_background

 

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/radiobutton_background_unchecked" android:state_checked="false" />

    <item android:drawable="@drawable/radiobutton_background_checked" android:state_checked="true" />

</selector>
radiobutton_background_unchecked
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 背景填充 -->
    <solid android:color="@color/white" />
    <!-- 圆角 -->
    <corners android:radius="5dp" />
    <stroke
        android:width="3dp"
        android:color="@color/color_text_gray" /><!-- 描边,边框宽度、颜色 -->

</shape>
radiobutton_background_checked
<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <!-- 填充 -->
    <solid android:color="#f59a23" />
    <!-- 圆角 -->
    <corners android:radius="5dp" />
<!--    <stroke-->
<!--        android:width="3dp"-->
<!--        android:color="@color/color_text_gray" />&lt;!&ndash; 描边,边框宽度、颜色 &ndash;&gt;-->

</shape>
radiobutton_textcolor
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_checked="true" />
    <item android:color="@color/black" android:state_checked="false" />

</selector>

 

 

Android RadioGroup动态添加RadioButton,设置margin_guopeng_233的博客-CSDN博客

使用RadioGroup动态添加RadioButton的过程,解决RadioButton单选冲突问题 - 简书android中radioGroup动态添加radioButton_Afanbaby的博客-CSDN博客

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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 添加RadioGroupradioGroup.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、付费专栏及课程。

余额充值