微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

简述RadioGroup和RadioButton的使用

简述RadioGroup和RadioButton的使用

在项目中有身份选择的选项,需要用到RadioGroup和RadioButton,这里做个记录,和大家交流一下。

话不多说,一起看代码

 

XML代码

 

<RadioGroup android:id="@+id/login_radiogroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal">

            <RadioButton android:id="@+id/admin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:text="@string/admin"/>

            <RadioButton android:id="@+id/tech" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:text="@string/tech"/>

            <RadioButton android:id="@+id/market" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:text="@string/market"/>

            <RadioButton android:id="@+id/guest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:text="@string/guest"/>

        </RadioGroup>

 

 

 

 

这里使用了layout_gravity让RadioGroup居中显示,使用了orientation属性,horizontal让内部的RadioButton呈水平摆放,vertical就是垂直摆放。

 

 

JAVA代码

 

 

这里因为在Fragment里面实例化布局,所有用了view.findViewById(),在Activity中的话去掉view就可以。

 

private RadioGroup mRadioGroup;

 

 

 

 

mRadioGroup=(RadioGroup)view.findViewById(R.id.login_radiogroup);

 

 

 

 

 

接下来在活动中实现对RadioGroup的监听。

 1 mRadioGroup.setonCheckedchangelistener(new CheckListener());  2 
 3 class CheckListener implements RadioGroup.OnCheckedchangelistener{  4 
 5  @Override  6         public void onCheckedChanged(RadioGroup group,int checkedId) {  7             switch (checkedId){  8                 case R.id.admin:  9                     //执行具体操作
10                     break; 11 
12                 case R.id.tech: 13                     //执行具体操作
14                     break; 15 
16                 case R.id.market: 17                     //执行具体操作
18                     break; 19 
20                 case R.id.guest: 21                     //执行具体操作
22                     break; 23 
24                 default: 25                     break; 26 
27  } 28  } 29     }

 

 

 

是个菜鸟,有错误还希望大家能指出来。

欢迎大家有好的想法一起交流。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。