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

首頁(yè) > 系統(tǒng) > Android > 正文

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

2020-04-11 12:43:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
學(xué)習(xí)目的:

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



RadioButton和CheckBox的區(qū)別:

1、單個(gè)RadioButton在選中后,通過(guò)點(diǎn)擊無(wú)法變?yōu)槲催x中
單個(gè)CheckBox在選中后,通過(guò)點(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布局:

復(fù)制代碼 代碼如下:

<?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)聽(tīng):

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

如果您對(duì)監(jiān)聽(tīng)器不熟悉,可以閱讀Android控件系列之Button以及Android監(jiān)聽(tīng)器

后臺(tái)代碼如下:
復(fù)制代碼 代碼如下:

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)聽(tīng)器
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)聽(tīng)事件。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 弋阳县| 贡山| 长春市| 无为县| 扶余县| 凤山县| 阜南县| 襄垣县| 石家庄市| 偃师市| 崇信县| 龙游县| 安丘市| 康马县| 丽江市| 资源县| 郯城县| 定远县| 武安市| 阿鲁科尔沁旗| 五大连池市| 易门县| 霍山县| 淮阳县| 新竹市| 甘孜| 嘉定区| 资源县| 高要市| 库尔勒市| 汝南县| 建始县| 顺昌县| 石台县| 蛟河市| 开江县| 肃北| 秭归县| 佛山市| 伊金霍洛旗| 玛纳斯县|