打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
android控件 RadioButton
RadioGroup和RadioButton一般是一起使用的。一般一个RadioGroup中间包含两个或以上的RadioButton。我之所以把这个控件单独拿出来讨论是因为它的初始化和一般的控件有一点区别。
1)初始化一个RadioGroup
group1=(RadioGroup) findViewById(R.id.radioGroup1);
2)添加group1的监听事件:
group1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
   @Override
   public void onCheckedChanged(RadioGroup group, int checkedId) {
    int radiobtId = group.getCheckedRadioButtonId();
    RadioButton rb=(RadioButton)findViewById(radiobtId);
    Log.i("msg", "radiobtId="+radiobtId);
    gender = String.valueOf(rb.getText());
   }  
  });
你会发现在监听函数中,我们根据当前选择的groupbutton的ID来初始化RadioButton,并得到它的值。
 
布局文件如下:
<RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <RadioButton
                android:id="@+id/radiomale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="男" />
            <RadioButton
                android:id="@+id/radiofemale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女" />
        </RadioGroup>

RadioButton和RadioGroup的关系:

1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器

2、每个RadioGroup中的RadioButton同时只能有一个被选中

3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中

4、大部分场合下,一个RadioGroup中至少有2个RadioButton

5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android RadioButton控件
一步一步android(8):关于界面控件的学习2【edittext、radiogroup、checkbox】
【Android自定义控件】支持多层嵌套RadioButton的RadioGroup
Android软件开发之盘点常用系统控件界面大合集(三)
单选按钮 RadioButton 的应用
android – 如何只设置一个RadioButton可以在RadioGroup中选择
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服