Android控制項系列之RadioButton與RadioGroup使用方法 詳細出處參考:http://www.jb51.net/article/31799.htm

來源:互聯網
上載者:User

學習目的:

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布局:

 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> 

選中項變更的事件監聽:

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

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

後台代碼如下:

 1 TextView tv = null;//根據不同選項所要變更的文本控制項  2 @Override  3 public void onCreate(Bundle savedInstanceState) {  4 super.onCreate(savedInstanceState);  5 setContentView(R.layout.main);  6 //根據ID找到該文本控制項  7 tv = (TextView)this.findViewById(R.id.tvSex);  8 //根據ID找到RadioGroup執行個體  9 RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup); 10 //綁定一個匿名監聽器 11 group.setOnCheckedChangeListener(new OnCheckedChangeListener() { 12 13 @Override 14 public void onCheckedChanged(RadioGroup arg0, int arg1) { 15 // TODO Auto-generated method stub 16 //擷取變更後的選中項的ID 17 int radioButtonId = arg0.getCheckedRadioButtonId(); 18 //根據ID擷取RadioButton的執行個體 19 RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId); 20 //更新常值內容,以符合選中項 21 tv.setText("您的性別是:" + rb.getText()); 22 } 23 }); 24 } 

效果如下:

 

詳細出處參考:http://www.jb51.net/article/31799.htm

總結:

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

詳細出處參考:http://www.jb51.net/article/31799.htm

詳細出處參考:http://www.jb51.net/article/31799.htm

 

詳細出處參考:http://www.jb51.net/article/31799.htm

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.