一、RadioButton和RadioGroup:
RadioButton是單個的圓形單選框,而RadioGroup是可以容納多個RadioButton存在的容器,因此RadioButton和RadioGroup往往都配合使用。
每個已經放入RadioGroup中的RadioButton只能有一個被選中,不放入RadioGroup中的RadioButton可以多選,和checkbox無異。
1、簡單實例:
<RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true"> <RadioButton android:id="@+id/buttonSchool1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/school1" android:checked="true"/> <RadioButton android:id="@+id/buttonSchool2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/school2"/> <RadioButton android:id="@+id/buttonSchool3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/school3"/> <RadioButton android:id="@+id/buttonSchool4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/school4"/> </RadioGroup>
運行結果如下:

2、RadioGroup基本屬性:
(1)、orientation:排列方式
若值為horizontal,則為橫向,水平排列:
android:orientation="horizontal"


若值為vertical,則為縱向,垂直排列。
android:orientation="vertical"


(2)、checkedButton:默認選中
直接調用已經放入在radiogroup中且已有id的radiobutton即可默認選中此項。
android:checkedButton="@+id/buttonSchool2"


3、RadioButton基本屬性:
(1)、checked:選中狀態
若為true則默認被選中,false則默認不被選中。


(2)、text等相關屬性:
text是按鈕的文本內容;
textSize是文本字體大小;
textColor是文本字體顏色······
這些屬性和TextView一致。


(3)、button:按鈕屬性
若button的值設為“@null”則不顯示前面的圓形按鈕,只顯示文本內容本身
android:button="@null"


二、CheckBox:
1、簡單實例:
<CheckBox android:text="@string/school1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/checkBox" /> <CheckBox android:text="@string/school2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/checkBox" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="19dp" android:id="@+id/checkBox2" /> <CheckBox android:text="@string/school3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/checkBox2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="23dp" android:id="@+id/checkBox3" />

2、基本屬性:
checkbox和radiobutton的屬性基本一致。
(1)、checked:是否被默認選中
android:checked="true"


(2)、text等相關屬性:


(3)、button:按鈕屬性
若button的值設為“@null”則不顯示前面的方形按鈕,只顯示文本內容本身

button屬性對按鈕的設置可以搭配drawable對按鈕的樣式進行修改和美化。
三、CheckBox和RadioButton區別:
| RadioButton | CheckBox |
| 選中后,通過點擊無法變為未選中 | 選中后,通過點擊可以變為未選中 |
| 只能同時選中一個 | 能同時選中多個 |
| 大部分UI框架中,默認圓形表示 | 大部分UI框架中,默認方形表示 |
