編輯:關於 Android 程式設計
在 Android 的表單設計裡,RadioButton(單選按鈕)與 CheckBox(複選框)是最常見的兩種選擇型元件。它們外觀相似、用法接近,但承載的互動邏輯截然不同。搞懂兩者的差異,是打好 UI 基礎的第一步。
1. 單獨一個 RadioButton 一旦選中,再點擊也無法取消;而單獨一個 CheckBox 選中後,再點一次即可反選。
2. 同一組 RadioButton 只能選中其中一個;同一組 CheckBox 則可同時勾選多項。
3. 在多數 UI 框架中,RadioButton 預設以圓形呈現,CheckBox 預設以方形(帶勾選標記)呈現。
除了「單選」與「多選」這條主線,以及事件處理方式略有不同之外,兩者在屬性與用法上有大量相似之處。
單選按鈕顧名思義只能選中一個選項,因此我們需要把多個 RadioButton 放進 RadioGroup 這個容器裡,才能實現「互斥單選」的效果。此外,可以透過 RadioGroup 的 orientation 屬性來決定內部按鈕是垂直排列還是水平排列。
XML 佈局範例:
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/btnMan"
android:checked="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/btnWoman"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="女" />
</RadioGroup>
<Button
android:id="@+id/btnpost"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="提交" />
</LinearLayout>
特別提醒:如果你想在 RadioButton 上使用
checked屬性設定預設選項,就必須為該控制項加上id。使用checked後,同一組中只會有一個選項處於選中狀態。
1. RadioButton 代表單個圓形單選框,RadioGroup 則是容納多個 RadioButton 的容器。
2. 每個 RadioGroup 內,同一時間只能有一個 RadioButton 被選中。
3. 不同 RadioGroup 之間互不干涉——A 組選了一個,B 組依然可以各自選一個。
4. 實務上,一個 RadioGroup 通常至少包含兩個 RadioButton。
5. 多數情況下,RadioGroup 會預設選中一項,建議把它放在群組的起始位置。
做法一:為 RadioGroup 設定 setOnCheckedChangeListener 事件監聽器,即時掌握切換。
private RadioGroup sex_Rg;
private String sex = "男";
sex_Rg = (RadioGroup) findViewById(R.id.sex_Rg);
sex_Rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.male_Rb) {
Toast.makeText(MainActivity.this, "男", Toast.LENGTH_SHORT).show();
sex = "男";
} else if (checkedId == R.id.female_Rb) {
Toast.makeText(MainActivity.this, "女", Toast.LENGTH_SHORT).show();
sex = "女";
}
}
});
做法二:透過點擊其他按鈕(例如「提交」)時,遍歷整個 RadioGroup 找出被選中的那一項。
Button btnchange = (Button) findViewById(R.id.btnpost);
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
btnchange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < radgroup.getChildCount(); i++) {
RadioButton rd = (RadioButton) radgroup.getChildAt(i);
if (rd.isChecked()) {
Toast.makeText(getApplicationContext(),
"點擊提交按鈕,你選擇的是:" + rd.getText(),
Toast.LENGTH_LONG).show();
break;
}
}
}
});
程式碼解析:這裡為提交按鈕設定了
setOnClickListener,每次點擊就遍歷一次 RadioGroup,判斷哪個按鈕被選中。常用方法包括:
getChildCount()取得單選按鈕數量;
getChildAt(i)依索引取出單選按鈕;
isChecked()判斷按鈕是否被選中。
複選框顧名思義,可以同時勾選多個選項,很適合用來收集「興趣愛好」這類多選資料。
XML 檔案範例:
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="愛好:" />
<CheckBox
android:id="@+id/game_Cb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="遊戲" />
<CheckBox
android:id="@+id/basketball_Cb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="籃球" />
<CheckBox
android:id="@+id/music_Cb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="音樂" />
</LinearLayout>
<Button
android:id="@+id/submit_Btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="提交" />
</LinearLayout>
取得選中值的兩種做法:
1. 為每個 CheckBox 加上 setOnCheckedChangeListener,即時回應勾選變化;
2. 準備一個按鈕,點擊後逐一判斷每個 CheckBox 的 isChecked() 狀態。
private Button submit_Btn;
private CheckBox game_Cb, basketball_Cb, music_Cb;
private String game = "", basketball = "", music = "";
submit_Btn = (Button) findViewById(R.id.submit_Btn);
game_Cb = (CheckBox) findViewById(R.id.game_Cb);
basketball_Cb = (CheckBox) findViewById(R.id.basketball_Cb);
music_Cb = (CheckBox) findViewById(R.id.music_Cb);
game_Cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// isChecked:當前控制項是否被選中,true 代表選中
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
game = isChecked ? "遊戲" : "";
}
});
basketball_Cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
basketball = isChecked ? "籃球" : "";
}
});
music_Cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
music = isChecked ? "音樂" : "";
}
});
submit_Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((game + basketball + music).equals("")) {
Toast.makeText(MainActivity.this, "沒有愛好", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"您的愛好有 " + game + " " + basketball + " " + music,
Toast.LENGTH_SHORT).show();
}
}
});

Step 1.
android:button="@null"
Step 2.android:drawableTop="@android:drawable/btn_radio"
其中的 drawable 也可以替換成自訂的圖示,做出個人化效果。
1. 在 XML 中透過
android:paddingXxx="xxx"控制間距;
2. 在 Java 程式碼中動態計算paddingLeft,彈性更高。
rb.setButtonDrawable(R.drawable.rad_btn_selctor); int rb_paddingLeft = getResources().getDrawable(R.mipmap.ic_checkbox_checked).getIntrinsicWidth() + 5; rb.setPadding(rb_paddingLeft, 0, 0, 0);
Android 四大組件入門:Activity 全解析
Activity 是 Android 應用中最常見的四大組件之一,負責承接使用者可見的介面,並管理頁面的建立、顯示、暫停與銷毀。它也是許多人接觸 Android 開發時,
Android Spinner 下拉選單控件的完整用法
一、效果預覽 二、建立版面檔(main.xml) 三、準備下拉選單的資料來源 List list = new ArrayList(); list.add(蘋果);
Android 自定義 View 實現圓形圖片效果
今天要講的是如何在 xml 文件中通過引用自定義的 View,實現 ImageView 的圓形圖片效果。首先在項目中新建一個類,我命名為 CircleImageView;
Android 熱修補丁方案設計與持續交付實踐
Android 的碎片化不僅體現在系統版本與機型上,也體現在發行渠道上——每個市場都有自己的政策與審核節奏。一次版本發布,對開發同學來說往往是漫長的煎熬。iOS 兩三天就