Android控件系列之RadioButtonRadioGroup
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
學(xué)習(xí)目的:
1、掌握在Android中如何建立RadioGroup和RadioButton
2、掌握RadioGroup的常用屬性
3、理解RadioButton和CheckBox的區(qū)別
4、掌握RadioGroup選中狀態(tài)變換的事件(監(jiān)聽器)
?
RadioButton和CheckBox的區(qū)別:
1、單個RadioButton在選中后,通過點擊無法變?yōu)槲催x中
??? 單個CheckBox在選中后,通過點擊可以變?yōu)槲催x中
2、一組RadioButton,只能同時選中一個
???? 一組CheckBox,能同時選中多個
3、RadioButton在大部分UI框架中默認(rèn)都以圓形表示
???? CheckBox在大部分UI框架中默認(rèn)都以矩形表示
RadioButton和RadioGroup的關(guān)系:
1、RadioButton表示單個圓形單選框,而RadioGroup是可以容納多個RadioButton的容器
2、每個RadioGroup中的RadioButton同時只能有一個被選中
3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個選中了,組B中依然可以有一個被選中
4、大部分場合下,一個RadioGroup中至少有2個RadioButton
5、大部分場合下,一個RadioGroup中的RadioButton默認(rèn)會有一個被選中,并建議您將它放在RadioGroup中的起始位置
XML布局:
?
1 <?xml version="1.0" encoding="utf-8"?>
2 ?<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 ? ?android:orientation="vertical"
4 ? ?android:layout_width="fill_parent"
5 ? ?android:layout_height="fill_parent"
6 ? ?>
7 ?<TextView ?
8 ? ?android:layout_width="fill_parent"
9 ? ?android:layout_height="wrap_content"
10 ? ?android:text="請選擇您的性別:"
11 ? ?android:textSize="9pt"
12 ? ?/>
13 ?<RadioGroup android:id="@+id/radioGroup" android:contentDescription="性別" android:layout_width="wrap_content" android:layout_height="wrap_content">
14 ? ?<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioMale" android:text="男" android:checked="true"></RadioButton>
15 ? ?<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioFemale" android:text="女"></RadioButton> ? ?
16 ?</RadioGroup>
17 <TextView ?
18 ? ?android:id="@+id/tvSex"
19 ? ?android:layout_width="fill_parent"
20 ? ?android:layout_height="wrap_content"
21 ? ?android:text="您的性別是:男"
22 ? ?android:textSize="9pt"
23 ? ?/>
24 </LinearLayout>
?
選中項變更的事件監(jiān)聽:
當(dāng)RadioGroup中的選中項變更后,您可能需要做一些相應(yīng),比如上述例子中,性別選擇“女”后下面的本文也相應(yīng)改變,又或者選擇不同的性別后,出現(xiàn)符合該性別的頭像列表進(jìn)行更新,女生不會喜歡使用大胡子作為自己的頭像。
如果您對監(jiān)聽器不熟悉,可以閱讀Android控件系列之Button以及Android監(jiān)聽器。
后臺代碼如下:
?
1 TextView tv = null;//根據(jù)不同選項所要變更的文本控件
2 ? ?@Override
3 ? ?public void onCreate(Bundle savedInstanceState) {
4 ? ? ? ?super.onCreate(savedInstanceState);
5 ? ? ? ?
6 ? ? ? ?setContentView(R.layout.main);
7 ? ? ? ?
8 ? ? ? ?//根據(jù)ID找到該文本控件
9 ? ? ? ?tv = (TextView)this.findViewById(R.id.tvSex);
10 ? ? ? ?//根據(jù)ID找到RadioGroup實例
11 ? ? ? ?RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup);
12 ? ? ? ?//綁定一個匿名監(jiān)聽器
13 ? ? ? ?group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
14 ? ? ? ? ? ?
15 ? ? ? ? ? ?@Override
16 ? ? ? ? ? ?public void onCheckedChanged(RadioGroup arg0, int arg1) {
17 ? ? ? ? ? ? ? ?// TODO Auto-generated method stub
18 ? ? ? ? ? ? ? ?//獲取變更后的選中項的ID
19 ? ? ? ? ? ? ? ?int radioButtonId = arg0.getCheckedRadioButtonId();
20 ? ? ? ? ? ? ? ?//根據(jù)ID獲取RadioButton的實例
21 ? ? ? ? ? ? ? ?RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId);
22 ? ? ? ? ? ? ? ?//更新文本內(nèi)容,以符合選中項
23 ? ? ? ? ? ? ? ?tv.setText("您的性別是:" + rb.getText());
24 ? ? ? ? ? ?}
25 ? ? ? ?});
26 ? ?}
效果如下:
總結(jié):
本文介紹了Android中如何使用RadioGroup和RadioButton,對比了RadioButton和CheckBox的區(qū)別,并實現(xiàn)了自定義的RadioGroup中被選中RadioButton的變更監(jiān)聽事件。
轉(zhuǎn)載于:https://my.oschina.net/u/1036767/blog/213621
總結(jié)
以上是生活随笔為你收集整理的Android控件系列之RadioButtonRadioGroup的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle tuning 工具
- 下一篇: C#设计模式系列:原型模式(Protot