加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

简述RadioGroup和RadioButton的使用

发布时间:2020-12-16 23:05:08 所属栏目:百科 来源:网络整理
导读:简述RadioGroup和RadioButton的使用 在项目中有身份选择的选项,需要用到RadioGroup和RadioButton,这里做个记录,和大家交流一下。 话不多说,一起看代码 ? XML代码 ? RadioGroup android:id ="@+id/login_radiogroup" android:layout_width ="wrap_content

简述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     }

?

?

?

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读