Android studio中Radiobutton 和 Radiogroup的使用详解

1.Radiobutton 和 Radiogroup的解析(单选框)

Radiobutton 是一个单选按钮,它表示一组选项中的一个。它通常与其他 Radiobutton 控件组合在一起,形成一个 Radiogroup。

Radiogroup 是一个容器控件,用于将多个 Radiobutton 组织在一起。Radiogroup 保证了其中的 Radiobutton 只能选择一个,即单选功能。 注意:Radiogroup具有和LinearLayout一样的横向属性(android:orientation="horizontal")和垂直属性(android:orientation="vertical")

2.Radiobutton的常用属性:

  • android:enabled:指定 Radiobutton 是否可用,值为 "true" 或 "false"
  • android:checked:指定 Radiobutton 的初始选中状态,值为 "true" 或 "false"

3.示例代码效果

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单选"/>

    <RadioGroup
        android:id="@+id/sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:checked="true"
            android:id="@+id/sex_boy"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:id="@+id/sex_girl"/>
    </RadioGroup>
 findcheck();
    sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
           switch (checkedId){
               case R.id.sex_boy:
                   Toast.makeText(RadiogroupActivity.this,"男", Toast.LENGTH_SHORT).show();
                   break;
               case R.id.sex_girl:
                   Toast.makeText(RadiogroupActivity.this,"女", Toast.LENGTH_SHORT).show();
                   break;
           }

        }
    });
}

private void findcheck(){
    sex=findViewById(R.id.sex);
}

4.效果图展示

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android StudioRadioButtonRadioGroup是常用的单选控件。RadioButton用于单个选项,而RadioGroup用于将多个RadioButton组合在一起,以便用户可以从选择一个选项。 以下是一个使用RadioGroupRadioButton的示例: 1. 在XML布局文件添加RadioGroupRadioButton: ```xml <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 3" /> </RadioGroup> ``` 2. 在Java代码获取RadioGroupRadioButton,并设置选的选项: ```java RadioGroup radioGroup = findViewById(R.id.radioGroup); RadioButton radioButton1 = findViewById(R.id.radioButton1); RadioButton radioButton2 = findViewById(R.id.radioButton2); RadioButton radioButton3 = findViewById(R.id.radioButton3); // 设置默认选的选项 radioButton1.setChecked(true); // 监听选项变化 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radioButton1: // 选了Option 1 break; case R.id.radioButton2: // 选了Option 2 break; case R.id.radioButton3: // 选了Option 3 break; } } }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小舒卿雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值