Android控件RadioButton的使用方法

 更新时间:2021年05月12日 10:48:38   作者:紧张的无痕  
这篇文章主要为大家详细介绍了Android控件RadioButton的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android控件RadioButton的使用代码,供大家参考,具体内容如下

内容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RadioActivity">
    <RadioGroup //定义一个单选按钮组
        android:id="@+id/rg_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton //单选按钮一 使用默认样式
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男"
            android:textSize="24sp"
            android:textColor="@color/black"/>
        <RadioButton //单选按钮2 使用默认样式
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="24sp"
            android:textColor="@color/black"/>
    </RadioGroup>
    <RadioGroup //组2
        android:id="@+id/rg_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/rg_1"
        android:layout_marginTop="50dp">
        <RadioButton
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="男"
            android:button="@null" //无按钮样式
            android:textSize="24sp"
            android:background="@drawable/selector_radiobutton" //自定义背景
            android:textColor="@color/black"
            android:checked="true"
            android:gravity="center"/>
        <RadioButton
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:button="@null" //无按钮样式
            android:text="女"
            android:background="@drawable/selector_radiobutton" //自定义背景
            android:textSize="24sp"
            android:textColor="@color/black"/>
    </RadioGroup>
</RelativeLayout>

//selector_radiobutton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"> //单选被选中的样式
        <shape android:shape="rectangle">
            <solid android:color="#ff66ff"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:state_checked="false"> //单选没被选中的样式
        <shape android:shape="rectangle">
            <stroke android:color="#cc33ff" android:width="2dp"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>
public class RadioActivity extends AppCompatActivity {
 
    private RadioGroup rg_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio);
        rg_1 = findViewById(R.id.rg_1);
        rg_1.setOnCheckedChangeListener((group, checkedId) -> {//设置组中单选按钮选中事件
            RadioButton radioButton = findViewById(checkedId);//获取被选中的id
            Toast.makeText(RadioActivity.this,radioButton.getText(),Toast.LENGTH_SHORT)
                    .show();//吐司一下被选中的文本值
        });
    }
}

运行效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android实现抽奖转盘实例代码

    Android实现抽奖转盘实例代码

    这篇文章主要介绍了Android实现抽奖转盘实例代码,可以应用于Android游戏开发中的一个应用,需要的朋友可以参考下
    2014-07-07
  • Android编程读取Assets所有文件(遍历每一个文件夹)并存入sdcard的方法

    Android编程读取Assets所有文件(遍历每一个文件夹)并存入sdcard的方法

    这篇文章主要介绍了Android编程读取Assets所有文件(遍历每一个文件夹)并存入sdcard的方法,涉及Android针对文件与目录的遍历及I/O操作相关技巧,需要的朋友可以参考下
    2016-02-02
  • 微信小程序 实现列表刷新的实例详解

    微信小程序 实现列表刷新的实例详解

    这篇文章主要介绍了微信小程序 实现列表刷新的实例详解的相关资料,这里提供了实现代码及实现效果图,需要的朋友可以参考下
    2016-11-11
  • android打开rar压缩文件

    android打开rar压缩文件

    这篇文章主要介绍了android打开rar压缩文件示例,调用RAR for android 打开压缩文件,需要的朋友可以参考下
    2014-03-03
  • android如何默认打开小区广播具体实现

    android如何默认打开小区广播具体实现

    小区广播的开关,1是打开,0是关闭;0x00就默认关闭,改成0x01就是默认打开,具体修改如下,感兴趣的朋友可以参考下哈
    2013-06-06
  • Android实现登录功能demo示例

    Android实现登录功能demo示例

    这篇文章主要介绍了Android实现登录功能demo示例,涉及登录信息操作、界面布局、登录逻辑判断等相关操作技巧,需要的朋友可以参考下
    2016-07-07
  • Android实现NFC读取校园卡

    Android实现NFC读取校园卡

    这篇文章主要为大家详细介绍了Android实现NFC读取校园卡,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • android studio后台服务使用详解

    android studio后台服务使用详解

    这篇文章主要为大家详细介绍了android studio后台服务,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • Android开发中CheckBox的简单用法示例

    Android开发中CheckBox的简单用法示例

    这篇文章主要介绍了Android开发中CheckBox的简单用法,结合实例形式分析了Android中CheckBox控件的基本功能设置与页面布局技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • Android简单实用的可拖拽GridView组件分享

    Android简单实用的可拖拽GridView组件分享

    在我们日常开发中,使用 GridView 这种网格视图的场合还是不少的,本篇我们来介绍一个支持拖拽的 GridView 组件,可以轻松搞定网格视图的拖拽排序,需要的可以参考一下
    2023-06-06

最新评论