Android控件系列之RadioButton與RadioGroup使用方法
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布局:
<?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="請選擇您的性別:"
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>
選中項變更的事件監(jiān)聽:
當(dāng)RadioGroup中的選中項變更后,您可能需要做一些相應(yīng),比如上述例子中,性別選擇“女”后下面的本文也相應(yīng)改變,又或者選擇不同的性別后,出現(xiàn)符合該性別的頭像列表進(jìn)行更新,女生不會喜歡使用大胡子作為自己的頭像。
如果您對監(jiān)聽器不熟悉,可以閱讀Android控件系列之Button以及Android監(jiān)聽器。
后臺代碼如下:
TextView tv = null;//根據(jù)不同選項所要變更的文本控件
@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實例
RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup);
//綁定一個匿名監(jiān)聽器
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
//獲取變更后的選中項的ID
int radioButtonId = arg0.getCheckedRadioButtonId();
//根據(jù)ID獲取RadioButton的實例
RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId);
//更新文本內(nèi)容,以符合選中項
tv.setText("您的性別是:" + rb.getText());
}
});
}
效果如下:
總結(jié):
本文介紹了Android中如何使用RadioGroup和RadioButton,對比了RadioButton和CheckBox的區(qū)別,并實現(xiàn)了自定義的RadioGroup中被選中RadioButton的變更監(jiān)聽事件。
- Android單選按鈕RadioButton的使用詳解
- Android控件RadioButton實現(xiàn)多選一功能
- Android開發(fā)設(shè)置RadioButton點擊效果的方法
- Android編程實現(xiàn)自定義PopupMenu樣式示例【顯示圖標(biāo)與設(shè)置RadioButton圖標(biāo)】
- Android RadioButton 圖片位置與大小實例詳解
- Android RadioGroup和RadioButton控件簡單用法示例
- Android中設(shè)置RadioButton在文字右邊的方法實例
- android RadioButton和CheckBox組件的使用方法
- Android RadioButton單選框的使用方法
- Android定制RadioButton樣式三種實現(xiàn)方法
- Android控件RadioButton的使用方法
相關(guān)文章
Android中監(jiān)聽判斷網(wǎng)絡(luò)連接狀態(tài)的方法
這篇文章主要介紹了Android中監(jiān)聽判斷網(wǎng)絡(luò)連接狀態(tài)的方法,介紹了是否有網(wǎng)絡(luò)連接判斷、連接的類型和監(jiān)聽網(wǎng)絡(luò)狀態(tài)的方法,需要的朋友可以參考下2014-06-06Flutter源碼分析之自定義控件(RenderBox)指南
寫了兩天的flutter,發(fā)現(xiàn)控件樣式很多,flutter資源很少,本文在于實用性,可以減少頁面代碼,下面這篇文章主要介紹了Flutter源碼分析之自定義控件(RenderBox)的相關(guān)資料,需要的朋友可以參考下2021-08-08Android ImageButton自定義按鈕的按下效果的代碼實現(xiàn)方法分享
這篇文章主要介紹了Android ImageButton自定義按鈕的按下效果的代碼實現(xiàn)方法,需要的朋友可以參考下2014-02-02Android常用布局(FrameLayout、LinearLayout、RelativeLayout)詳解
這篇文章主要為大家詳細(xì)介紹了Android常用布局FrameLayout、LinearLayout、RelativeLayout,感興趣的小伙伴們可以參考一下2016-06-06基于adbkit的android設(shè)備管理(精簡版stf)
這篇文章主要為大家介紹了基于adbkit的android設(shè)備管理(精簡版stf)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09