radiobutton即選項按鈕,checkbox即複選按鈕也就是可以同時選擇多個選項。下面的程式定義了一組選項按鈕和三個複選框,來讓使用者選擇使用者相關的資訊,其中選項按鈕必須放在一個RadioGroup中才能實現單選的效果。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性別:"
android:textSize="11pt"
/>
<!-- 定義一組單選框 -->
<RadioGroup
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<!-- 定義兩個單選框 -->
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
/>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜歡的顏色:"
android:textSize="11pt"
/>
<!-- 定義一個垂直的線性布局 -->
<LinearLayout android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- 定義三個複選框 -->
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="紅色"
android:checked="true"
/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="藍色"
/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="綠色"
/>
</LinearLayout>
</TableRow>
</TableLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性別:"
android:textSize="11pt"
/>
<!-- 定義一組單選框 -->
<RadioGroup
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<!-- 定義兩個單選框 -->
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
/>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜歡的顏色:"
android:textSize="11pt"
/>
<!-- 定義一個垂直的線性布局 -->
<LinearLayout android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- 定義三個複選框 -->
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="紅色"
android:checked="true"
/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="藍色"
/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="綠色"
/>
</LinearLayout>
</TableRow>
</TableLayout>
摘自 hn307165411的專欄