radioButton控件的使用方法

radioButton控件的使用方法

radioButton控件的定义

private RadioGroup rg;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.explain);
    rg=(RadioGroup)findViewById(R.id.radio_group); //定义radioGroup控件
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {//表示RadioGroup中的radioButton状态切换时触发的监听
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
            RadioButton rb = (RadioButton)findViewById(checkedId); //选中radioButton的对象资源id获取radioButton对象
            String str = rb.getText().toString();//获取选中radioButton的文本
            Toast.makeText(Explain.this,"Hello"+str,Toast.LENGTH_SHORT).show();            			  
        }
     });
}

将radioButton圆点去除

    <style name="Radio_style">
        <item name="android:layout_width">0dp</item>
        <item name="android:padding">3dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:button">@null</item><!--去除RadioButton默认带的圆点-->
        <item name="android:gravity">center</item>
        <item name="android:textSize">12sp</item>
    </style>

布局xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_alignParentBottom="true">

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="#FFFFFF"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <RadioButton
            android:text="首页"
            android:textSize="20sp"
            style="@style/Radio_style"
            android:id="@+id/radioButton"
            android:background="@mipmap/ic_launcher"
            android:checked="false" />

        <RadioButton
            android:text="联系人"
            android:textSize="20sp"
            style="@style/Radio_style"
            android:id="@+id/radioButton2" />

        <RadioButton
            android:text="推荐"
            android:textSize="20sp"
            style="@style/Radio_style"
            android:id="@+id/radioButton3" />
        <RadioButton
            android:text="个人中心"
            android:textSize="20sp"
            style="@style/Radio_style"
            android:id="@+id/radioButton4" />
    </RadioGroup>


</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值