当前位置:网站首页>Radiogroup dynamically add RadioButton

Radiogroup dynamically add RadioButton

2022-06-11 18:06:00 xiaoerbuyu1233

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);
            // Set up RadioButton Margin  (int left, int top, int right, int bottom)
            lp.setMargins(SizeUtils.dp2px(10), SizeUtils.dp2px(10), SizeUtils.dp2px(10), SizeUtils.dp2px(10));
            // Set up RadioButton background 
            radioButton.setBackgroundResource(R.drawable.radiobutton_background);
radioButton.setTextColor(getResources().getColorStateList(R.drawable.radiobutton_textcolor));
            radioButton.setButtonDrawable(null);
            // Set the distance around the text 
            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);
            // Set text 
            radioButton.setText(i + " # pot ");
            final int finalI = i;
            // Set up radioButton Click events for 
            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();
                }
            });
            // take radioButton Add to radioGroup in 
            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=" Options 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=" Options 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">
    <!--  Background fill  -->
    <solid android:color="@color/white" />
    <!--  Round corners  -->
    <corners android:radius="5dp" />
    <stroke
        android:width="3dp"
        android:color="@color/color_text_gray" /><!--  Stroke , Border width 、 Color  -->

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

    android:shape="rectangle">

    <!--  fill  -->
    <solid android:color="#f59a23" />
    <!--  Round corners  -->
    <corners android:radius="5dp" />
<!--    <stroke-->
<!--        android:width="3dp"-->
<!--        android:color="@color/color_text_gray" />&lt;!&ndash;  Stroke , Border width 、 Color  &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 Dynamic addition RadioButton, Set up margin_guopeng_233 The blog of -CSDN Blog

Use RadioGroup Dynamic addition RadioButton The process of , solve RadioButton Single choice conflict problem - Simple books android in radioGroup Dynamic addition radioButton_Afanbaby The blog of -CSDN Blog

原网站

版权声明
本文为[xiaoerbuyu1233]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111749343266.html