RadioGroup,Android的单选框。

在安卓上,实现单选框。
先看一下需求:在用户注册页面需要填性别项,大家知道性别不是男的便是女的。(这里不考虑其它情况)这时候单选框就派上用场了,先看布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
       <RadioGroup
        android:id="@+id/sexRadioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/man"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="男" />

        <RadioButton
            android:id="@+id/woman"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>


</LinearLayout>

java代码:
初始化选择男性

    man.setChecked(true);

监听选择的选项

sexRadioGroup.setOnCheckedChangeListener(sexRadioGroupchange);

单选框监听器

    private RadioGroup.OnCheckedChangeListener sexRadioGroupchange= new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId==man.getId()) {
                log.d(TAG,"man");
            }else  {
                log.d(TAG,"woman");
            }
        }
    };
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,单选框是一种常见的 UI 组件,用于从一组选项中选择一个选项。以下是创建单选框的步骤: 1. 在 XML 布局文件中添加 RadioGroup 和 RadioButton 组件。RadioGroup 用于包含一组单选钮,而 RadioButton 用于定义单选钮选项。例如: ``` <RadioGroup android:id="@+id/radio_group" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/radio_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" /> <RadioButton android:id="@+id/radio_button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 3" /> </RadioGroup> ``` 2. 在 Java 代码中获取 RadioGroup 对象,并设置监听器以获取所选单选钮的值。例如: ``` RadioGroup radioGroup = findViewById(R.id.radio_group); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { RadioButton radioButton = findViewById(i); String selectedOption = radioButton.getText().toString(); // Do something with the selected option } }); ``` 其中,onCheckedChanged() 方法在单选钮状态发生更改时被调用,i 参数是所选 RadioButton 的 ID。通过 findViewById() 方法获取 RadioButton 对象,然后使用 getText() 方法获取所选选项的文本。最后,可以使用所选选项执行某些操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值