Android控件系列之RadioButton&RadioGroup

<p>学习目的:</p><p>1、掌握在Android中如何建立RadioGroup和RadioButton</p><p>2、掌握RadioGroup的常用属性</p><p>3、理解RadioButton和CheckBox的区别</p><p>4、掌握RadioGroup选中状态变换的事件(监听器)</p><p><img src="/up_pic/201812/191105034200.jpg" title="191105034200.jpg" alt="1.jpg"/></p><p>RadioButton和CheckBox的区别:</p><p>1、单个RadioButton在选中后,通过点击无法变为未选中</p><p> 单个CheckBox在选中后,通过点击可以变为未选中</p><p>2、一组RadioButton,只能同时选中一个</p><p> 一组CheckBox,能同时选中多个</p><p>3、RadioButton在大部分UI框架中默认都以圆形表示</p><p> CheckBox在大部分UI框架中默认都以矩形表示</p><p>RadioButton和RadioGroup的关系:</p><p>1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器</p><p>2、每个RadioGroup中的RadioButton同时只能有一个被选中</p><p>3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中</p><p>4、大部分场合下,一个RadioGroup中至少有2个RadioButton</p><p>5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置</p><p>XML布局:</p><pre class="brush:xml;toolbar:false"> 1&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; 2&lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; 3android:orientation=&quot;vertical&quot; 4android:layout_width=&quot;fill_parent&quot; 5android:layout_height=&quot;fill_parent&quot; 6&gt; 7&lt;TextView 8android:layout_width=&quot;fill_parent&quot; 9android:layout_height=&quot;wrap_content&quot; 10android:text=&quot;请选择您的性别:&quot; 11android:textSize=&quot;9pt&quot; 12/&gt; 13&lt;RadioGroupandroid:id=&quot;@+id/radioGroup&quot;android:contentDescription=&quot;性别&quot;android:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot;&gt; 14&lt;RadioButtonandroid:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot;android:id=&quot;@+id/radioMale&quot;android:text=&quot;男&quot;android:checked=&quot;true&quot;&gt;&lt;/RadioButton&gt; 15&lt;RadioButtonandroid:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot;android:id=&quot;@+id/radioFemale&quot;android:text=&quot;女&quot;&gt;&lt;/RadioButton&gt; 16&lt;/RadioGroup&gt; 17&lt;TextView 18android:id=&quot;@+id/tvSex&quot; 19android:layout_width=&quot;fill_parent&quot; 20android:layout_height=&quot;wrap_content&quot; 21android:text=&quot;您的性别是:男&quot; 22android:textSize=&quot;9pt&quot; 23/&gt; 24&lt;/LinearLayout&gt;</pre><p>选中项变更的事件监听:</p><p>当RadioGroup中的选中项变更后,您可能需要做一些相应,比如上述例子中,性别选择&quot;女&quot;后下面的本文也相应改变,又或者选择不同的性别后,出现符合该性别的头像列表进行更新,女生不会喜欢使用大胡子作为自己的头像。</p><p>如果您对监听器不熟悉,可以阅读Android控件系列之Button以及Android监听器。</p><p>后台代码如下:</p><pre class="brush:java;toolbar:false"> 1TextViewtv=null;//根据不同选项所要变更的文本控件 2@Override 3publicvoidonCreate(BundlesavedInstanceState){ 4super.onCreate(savedInstanceState); 5 6setContentView(R.layout.main); 7 8//根据ID找到该文本控件 9tv=(TextView)this.findViewById(R.id.tvSex); 10//根据ID找到RadioGroup实例 11RadioGroupgroup=(RadioGroup)this.findViewById(R.id.radioGroup); 12//绑定一个匿名监听器 13group.setOnCheckedChangeListener(newOnCheckedChangeListener(){ 14 15@Override 16publicvoidonCheckedChanged(RadioGrouparg0,intarg1){ 17//TODOAuto-generatedmethodstub 18//获取变更后的选中项的ID 19intradioButtonId=arg0.getCheckedRadioButtonId(); 20//根据ID获取RadioButton的实例 21RadioButtonrb=(RadioButton)MyActiviy.this.findViewById(radioButtonId); 22//更新文本内容,以符合选中项 23tv.setText(&quot;您的性别是:&quot;+rb.getText()); 24} 25}); 26}</pre><p>效果如下:</p><p><img src="/up_pic/201812/191105094229.jpg" title="191105094229.jpg" alt="2.jpg"/></p><p>总结:</p><p>本文介绍了Android中如何使用RadioGroup和RadioButton,对比了RadioButton和CheckBox的区别,并实现了自定义的RadioGroup中被选中RadioButton的变更监听事件。</p>
RangeTime:0.007386s
RangeMem:211.62 KB
返回顶部 留言