标签:case 简述 break RadioButton RadioGroup 具体操作 id
简述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 }
是个菜鸟,有错误还希望大家能指出来。
欢迎大家有好的想法一起交流。
标签:case,简述,break,RadioButton,RadioGroup,具体操作,id 来源: https://www.cnblogs.com/hzauxx/p/11002763.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。