Android控件系列之RadioButton與RadioGroup使用方法
1、掌握在Android中如何建立RadioGroup和RadioButton
2、掌握RadioGroup的常用屬性
3、理解RadioButton和CheckBox的區(qū)別
4、掌握RadioGroup選中狀態(tài)變換的事件(監(jiān)聽器)

RadioButton和CheckBox的區(qū)別:
1、單個(gè)RadioButton在選中后,通過點(diǎn)擊無(wú)法變?yōu)槲催x中
單個(gè)CheckBox在選中后,通過點(diǎn)擊可以變?yōu)槲催x中
2、一組RadioButton,只能同時(shí)選中一個(gè)
一組CheckBox,能同時(shí)選中多個(gè)
3、RadioButton在大部分UI框架中默認(rèn)都以圓形表示
CheckBox在大部分UI框架中默認(rèn)都以矩形表示
RadioButton和RadioGroup的關(guān)系:
1、RadioButton表示單個(gè)圓形單選框,而RadioGroup是可以容納多個(gè)RadioButton的容器
2、每個(gè)RadioGroup中的RadioButton同時(shí)只能有一個(gè)被選中
3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個(gè)選中了,組B中依然可以有一個(gè)被選中
4、大部分場(chǎng)合下,一個(gè)RadioGroup中至少有2個(gè)RadioButton
5、大部分場(chǎng)合下,一個(gè)RadioGroup中的RadioButton默認(rèn)會(huì)有一個(gè)被選中,并建議您將它放在RadioGroup中的起始位置
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請(qǐng)選擇您的性別:"
android:textSize="9pt"
/>
<RadioGroup android:id="@+id/radioGroup" android:contentDescription="性別" android:layout_width="wrap_content" android:layout_height="wrap_content">
<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioMale" android:text="男" android:checked="true"></RadioButton>
<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioFemale" android:text="女"></RadioButton>
</RadioGroup>
<TextView
android:id="@+id/tvSex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="您的性別是:男"
android:textSize="9pt"
/>
</LinearLayout>
選中項(xiàng)變更的事件監(jiān)聽:
當(dāng)RadioGroup中的選中項(xiàng)變更后,您可能需要做一些相應(yīng),比如上述例子中,性別選擇“女”后下面的本文也相應(yīng)改變,又或者選擇不同的性別后,出現(xiàn)符合該性別的頭像列表進(jìn)行更新,女生不會(huì)喜歡使用大胡子作為自己的頭像。
如果您對(duì)監(jiān)聽器不熟悉,可以閱讀Android控件系列之Button以及Android監(jiān)聽器。
后臺(tái)代碼如下:
TextView tv = null;//根據(jù)不同選項(xiàng)所要變更的文本控件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根據(jù)ID找到該文本控件
tv = (TextView)this.findViewById(R.id.tvSex);
//根據(jù)ID找到RadioGroup實(shí)例
RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup);
//綁定一個(gè)匿名監(jiān)聽器
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
//獲取變更后的選中項(xiàng)的ID
int radioButtonId = arg0.getCheckedRadioButtonId();
//根據(jù)ID獲取RadioButton的實(shí)例
RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId);
//更新文本內(nèi)容,以符合選中項(xiàng)
tv.setText("您的性別是:" + rb.getText());
}
});
}
效果如下:

總結(jié):
本文介紹了Android中如何使用RadioGroup和RadioButton,對(duì)比了RadioButton和CheckBox的區(qū)別,并實(shí)現(xiàn)了自定義的RadioGroup中被選中RadioButton的變更監(jiān)聽事件。
- Android單選按鈕RadioButton的使用詳解
- Android控件RadioButton實(shí)現(xiàn)多選一功能
- Android開發(fā)設(shè)置RadioButton點(diǎn)擊效果的方法
- Android編程實(shí)現(xiàn)自定義PopupMenu樣式示例【顯示圖標(biāo)與設(shè)置RadioButton圖標(biāo)】
- Android RadioButton 圖片位置與大小實(shí)例詳解
- Android RadioGroup和RadioButton控件簡(jiǎn)單用法示例
- Android中設(shè)置RadioButton在文字右邊的方法實(shí)例
- android RadioButton和CheckBox組件的使用方法
- Android RadioButton單選框的使用方法
- Android定制RadioButton樣式三種實(shí)現(xiàn)方法
- Android控件RadioButton的使用方法
相關(guān)文章
Android中的HTextView庫(kù)實(shí)現(xiàn)TextView動(dòng)畫效果
HTextView是一個(gè)用來(lái)給TextView里的文字做各種轉(zhuǎn)換動(dòng)畫的開源庫(kù),不僅提供了多種動(dòng)畫選擇,而且還有重復(fù)字符的位移動(dòng)畫,雖然并沒有多么復(fù)雜,但是它使用的這些典型的設(shè)計(jì)模式以及各種動(dòng)畫的實(shí)現(xiàn)確實(shí)可以從中讓我們學(xué)到不少知識(shí)2023-12-12
Android組合控件實(shí)現(xiàn)功能強(qiáng)大的自定義控件
這篇文章主要介紹了Android組合控件實(shí)現(xiàn)功能強(qiáng)大的自定義控件的相關(guān)資料,需要的朋友可以參考下2016-05-05
Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程詳解
這篇文章主要介紹了Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-05-05
Android復(fù)選框?qū)υ捒蛴梅▽?shí)例簡(jiǎn)析
這篇文章主要介紹了Android復(fù)選框?qū)υ捒蛴梅?結(jié)合實(shí)例形式簡(jiǎn)單分析了Android復(fù)選對(duì)話框的創(chuàng)建與使用技巧,需要的朋友可以參考下2016-01-01
flutter仿微信底部圖標(biāo)漸變功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了flutter仿微信底部圖標(biāo)漸變功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android實(shí)現(xiàn)EditText圖文混合插入上傳功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)EditText圖文混合插入上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android利用POI實(shí)現(xiàn)將圖片插入到Excel
這篇文章主要為大家詳細(xì)介紹了Android如何利用POI實(shí)現(xiàn)將圖片插入到Excel,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
Android?App設(shè)計(jì)規(guī)范深入講解
隨著安卓智能手機(jī)不停的更新?lián)Q代,安卓手機(jī)系統(tǒng)越來(lái)越完美,屏幕尺寸也越來(lái)越大啦,下面這篇文章主要給大家介紹了關(guān)于Android?App設(shè)計(jì)規(guī)范的相關(guān)資料,需要的朋友可以參考下2022-10-10
android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼
本篇文章主要介紹了android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-08-08

