国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android控件系列之RadioButton與RadioGroup使用方法

2020-02-21 17:39:32
字體:
來源:轉載
供稿:網友
學習目的:

1、掌握在Android中如何建立RadioGroup和RadioButton
2、掌握RadioGroup的常用屬性
3、理解RadioButton和CheckBox的區別
4、掌握RadioGroup選中狀態變換的事件(監聽器)



RadioButton和CheckBox的區別:

1、單個RadioButton在選中后,通過點擊無法變為未選中
單個CheckBox在選中后,通過點擊可以變為未選中
2、一組RadioButton,只能同時選中一個
一組CheckBox,能同時選中多個
3、RadioButton在大部分UI框架中默認都以圓形表示
CheckBox在大部分UI框架中默認都以矩形表示
RadioButton和RadioGroup的關系:
1、RadioButton表示單個圓形單選框,而RadioGroup是可以容納多個RadioButton的容器
2、每個RadioGroup中的RadioButton同時只能有一個被選中
3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個選中了,組B中依然可以有一個被選中
4、大部分場合下,一個RadioGroup中至少有2個RadioButton
5、大部分場合下,一個RadioGroup中的RadioButton默認會有一個被選中,并建議您將它放在RadioGroup中的起始位置

XML布局:



android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請選擇您的性別:"
android:textSize="9pt"
/>




android:id="@+id/tvSex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="您的性別是:男"
android:textSize="9pt"
/>


選中項變更的事件監聽:

當RadioGroup中的選中項變更后,您可能需要做一些相應,比如上述例子中,性別選擇“女”后下面的本文也相應改變,又或者選擇不同的性別后,出現符合該性別的頭像列表進行更新,女生不會喜歡使用大胡子作為自己的頭像。

如果您對監聽器不熟悉,可以閱讀Android控件系列之Button以及Android監聽器。

后臺代碼如下:

TextView tv = null;//根據不同選項所要變更的文本控件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根據ID找到該文本控件
tv = (TextView)this.findViewById(R.id.tvSex);
//根據ID找到RadioGroup實例
RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup);
//綁定一個匿名監聽器
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
//獲取變更后的選中項的ID
int radioButtonId = arg0.getCheckedRadioButtonId();
//根據ID獲取RadioButton的實例
RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId);
//更新文本內容,以符合選中項
tv.setText("您的性別是:" + rb.getText());
}
});
}


效果如下:

總結:

本文介紹了Android中如何使用RadioGroup和RadioButton,對比了RadioButton和CheckBox的區別,并實現了自定義的RadioGroup中被選中RadioButton的變更監聽事件。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 博罗县| 昔阳县| 蒙阴县| 上犹县| 新泰市| 丹寨县| 昂仁县| 翁源县| 宁晋县| 普定县| 竹北市| 武义县| 松阳县| 佛坪县| 通道| 湾仔区| 湄潭县| 通城县| 大庆市| 彭水| 建昌县| 南郑县| 互助| 芷江| 林芝县| 加查县| 盖州市| 永新县| 磐安县| 山阴县| 喀喇沁旗| 巩义市| 新巴尔虎左旗| 永修县| 漳浦县| 英吉沙县| 临沭县| 信宜市| 衡南县| 金阳县| 马关县|