Android常用控件之RadioGroup

目录:android.widget.RadioGroup

xml布局中:(包含按钮选中状态及文本选中状态的改变)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:id="@+id/rg_radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_one"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:background="@drawable/selector_rb_bg"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:text="tab1"
            android:textColor="@drawable/selector_rb_color_bg" />

        <RadioButton
            android:id="@+id/rb_two"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:background="@drawable/selector_rb_bg"
            android:button="@null"
            android:gravity="center"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:text="tab1"
            android:textColor="@drawable/selector_rb_color_bg" />
    </RadioGroup>
</RelativeLayout>

按钮背景选择器:selector_rb_bg.xml

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

round_grey_press.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 填充颜色 -->
    <solid android:color="@color/holo_light_button_pressed" />
    <!-- 圆角度数 -->
    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
</shape>

round_grey_default.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 填充颜色 -->
    <solid android:color="@color/holo_light_button_normal" />
    <!-- 圆角度数 -->
    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
</shape>

res color:

<color name="holo_light_button_pressed">#66666666</color>
<color name="holo_light_button_normal">#b3cccccc</color>

按钮字体颜色选择器:selector_rb_color_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/holo_green_dark" android:state_checked="true" />
    <item android:color="@android:color/white" />
</selector>
代码使用:
public class MainActivity extends AppCompatActivity {

    private RadioGroup rg_radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        rg_radioGroup = (RadioGroup) findViewById(R.id.rg_radioGroup);
        //设置按钮组监听
        rg_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //选中位置 checkedId
            }
        });

        //通过代码手动设置选中按钮
        //RadioButton childAt = (RadioButton) rg_radioGroup.getChildAt(0);
        //childAt.setChecked(true);
    }

}

补充:通过代码编写RadioButton并设置相应效果:

/**新建单选按钮*/
private RadioButton getRadioButton(RadioGroup.LayoutParams rbParams, String rbTitle){
    final RadioButton rb = new RadioButton(context);
    rb.setText(rbTitle);
    rb.setTextAppearance(context, R.style.common_text_gray_style);//代码添加style
    rb.setTextColor(getResources().getColor(R.color.tab_default_bg));//注意被上面style字体颜色属性覆盖
    rb.setLayoutParams(rbParams);//width=60dp
    rb.setButtonDrawable(getResources().getDrawable(android.R.color.transparent));//android:button="@null"
    rb.setPadding(0, DensityUtils.dp2px(context, 5), 0, DensityUtils.dp2px(context, 5));
    rb.setBackgroundResource(R.drawable.selector_buy_rb_bg);
    rb.setGravity(Gravity.CENTER);
    rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //android:textColor="@drawable/selector_buy_rb_tv_color"
            if (isChecked)
                rb.setTextColor(getResources().getColor(R.color.white));
            else
                rb.setTextColor(getResources().getColor(R.color.tab_default_bg));
        }
    });
    return rb;
}
    原文作者:Hans在路上
    原文地址: https://www.jianshu.com/p/e40d3d43d85a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞