單選按鈕(RadioButton)和復選框(CheckBox)、狀態開關按鈕(ToggleButton)與開關(Switch)是用戶界面中最普通的UI組件,他們都繼承了Button類,因此都可直接使用Button支持的各種屬性和方法。
RadioButton、CheckBo與普通按鈕不同的是,它們多了一個可選中的功能,因此RadioButon、CheckBox都可額外指定一個android:checked屬性,該屬性用於指定RadioButton、CheckBox初始時是否被選中。
RadioButton與CheckBox的不同之處在於,一組RadioButton只能選中其中一個,因此RadioButton通常要與RadioGroup一起使用,用於定義一組單選按鈕。
實例:利用單選按鈕、復選框按鈕獲取用戶信息
在需要獲取用戶信息的界面中,有些信息不需要用戶直接輸入,可以考慮讓用戶進行選擇,比如用戶的性別、愛好等。下面的界面布局文件定義一個讓用戶選擇的輸入界面。
<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="16dp"/> <!-- 定義一組單選按鈕 --> <RadioGroup android:id="@+id/rg" android:orientation="horizontal" android:layout_gravity="center_horizontal"> <!-- 定義兩個單選按鈕 --> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/male" android:text="男" android:checked="true" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/female" android:text="女"/> </RadioGroup> </TableRow> <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜歡的顏色:" android:textSize="16dp"/> <!-- 定義一個垂直的線性布局 --> <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> <TextView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableLayout>
上面的界面布局中定義了一組單選按鈕,並默認勾選了第一個單選按鈕,這組單選按鈕供用戶選擇性別:還定義了三個復選框,供用戶選擇喜歡的顏色。
注意:如果在XML布局文件中默認勾選了某個單選按鈕,則必須為該組單選按鈕的每個按鈕指定android:id屬性值,否則這組單選按鈕不能正常工作。
為了監聽單選按鈕、復選框勾選狀態的改變,可以為它們添加事件監聽器。例如下面Activity為RadioGroup添加了事件監聽器,該監聽器可以監聽這組單選按鈕的勾選狀態的改變。
package org.crazyit.helloworld; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.*; import android.widget.RadioGroup.*; public class RaidoButtonCheckBoxTest extends Activity { RadioGroup rg; TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.raido_button_check_box_test); //獲取界面上rg、show兩個組件 rg=(RadioGroup)findViewById(R.id.rg); show=(TextView)findViewById(R.id.show); //為RadioGroup組件的OnCheck事件綁定事件監聽器 rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub //根據用戶勾選的單選按鈕來動態改變tip字符串的值 String tip=checkedId==R.id.male?"您的性別是男人":"您的性別是女人"; //修改show組件中的文本 show.setText(tip); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.raido_button_check_box_test, menu); return true; } }
運行上面的程序,並改變第一組單選按鈕的勾選狀態,將看到如圖2.25所示界面
圖2.25 單選按鈕、復選框示意圖
