編輯:關於 Android 程式設計
CheckBox 是介面開發中最常見的多選控制項。Android 的實作方式很直接:每個 CheckBox 各自掛一個 CompoundButton.OnCheckedChangeListener,在回呼中依 isChecked 決定要加還是要移除。本文用「選擇喜歡的水果」這個例子說明。
package com.zhuguangwei;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class ChkBoxActivity extends Activity {
private TextView myTextView;
private CheckBox myApple;
private CheckBox myOrange;
private CheckBox myBanana;
private CheckBox myWaterMelon;
private CheckBox myStrawBerry;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (TextView) findViewById(R.id.myTextView);
myApple = (CheckBox) findViewById(R.id.Apple);
myOrange = (CheckBox) findViewById(R.id.Orange);
myBanana = (CheckBox) findViewById(R.id.banana);
myWaterMelon = (CheckBox) findViewById(R.id.watermelon);
myStrawBerry = (CheckBox) findViewById(R.id.strawberry);
myApple.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
myTextView.append(myApple.getText().toString());
} else {
if (myTextView.getText().toString().contains("蘋果")) {
myTextView.setText(myTextView.getText().toString().replace("蘋果", ""));
}
}
}
});
myOrange.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
myTextView.append(myOrange.getText().toString());
} else {
if (myTextView.getText().toString().contains("橘子")) {
myTextView.setText(myTextView.getText().toString().replace("橘子", ""));
}
}
}
});
myBanana.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
myTextView.append(myBanana.getText().toString());
} else {
if (myTextView.getText().toString().contains("香蕉")) {
myTextView.setText(myTextView.getText().toString().replace("香蕉", ""));
}
}
}
});
myWaterMelon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
myTextView.append(myWaterMelon.getText().toString());
} else {
if (myTextView.getText().toString().contains("西瓜")) {
myTextView.setText(myTextView.getText().toString().replace("西瓜", ""));
}
}
}
});
myStrawBerry.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
myTextView.append(myStrawBerry.getText().toString());
} else {
if (myTextView.getText().toString().contains("草莓")) {
myTextView.setText(myTextView.getText().toString().replace("草莓", ""));
}
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請選擇一下你喜歡吃的水果:" />
<CheckBox android:id="@+id/Apple"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="蘋果" />
<CheckBox android:id="@+id/Orange"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="橘子" />
<CheckBox android:id="@+id/banana"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="香蕉" />
<CheckBox android:id="@+id/watermelon"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="西瓜" />
<CheckBox android:id="@+id/strawberry"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="草莓" />
</LinearLayout>

上面五段程式碼幾乎一模一樣,只差在字串。這種重複在教學範例中很常見,但正式專案應該收斂。有兩個方向:
讓 Activity 實作介面,所有 CheckBox 共用同一個回呼,再用 buttonView 參數判斷是誰:
public class ChkBoxActivity extends Activity
implements CompoundButton.OnCheckedChangeListener {
private TextView myTextView;
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String name = buttonView.getText().toString();
String current = myTextView.getText().toString();
if (isChecked) {
myTextView.setText(current + name);
} else {
myTextView.setText(current.replace(name, ""));
}
}
}
// 註冊時一次搞定
myApple.setOnCheckedChangeListener(this);
myOrange.setOnCheckedChangeListener(this);
myBanana.setOnCheckedChangeListener(this);
本例把「顯示文字」與「資料值」混在一起用,於是取消勾選時得靠 contains 與 replace 去反推,既脆弱又容易出錯(例如「橘子」與「橘子汽水」會互相干擾)。正確做法是維護一個獨立的狀態集合:
private final Set<String> selected = new HashSet<>();
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String key = (String) buttonView.getTag(); // 在 XML 用 android:tag 或程式碼設定
if (isChecked) {
selected.add(key);
} else {
selected.remove(key);
}
myTextView.setText("已選:" + selected.toString());
}
setChecked(true) 而尚未掛上監聽,順序會影響結果。若要在初始化時設定狀態卻不想觸發回呼,可先暫時移除監聽,設好後再接回。getView 都要重新 setChecked;而 setChecked 又會觸發監聽,容易導致迴圈。實務上會在設定前先 setOnCheckedChangeListener(null)。android:button 可換成自訂 drawable,android:buttonTint(API 21+)可改勾選色;若想要完全自繪的樣式,可以改用 AppCompatCheckBox 並搭配 buttonTint 的 color selector。總結:CheckBox 的技術門檻很低,真正的功夫在狀態管理。把「勾選狀態」存在一個明確的資料結構裡,而不是靠畫面上的文字反推,是新手與資深工程師最明顯的分野之一。
深入解析 Android 圖片資源、記憶體佔用與 drawable 目錄之間的關係
自從完成上一篇《Android 螢幕適配全攻略》之後,常有朋友問我一個問題:「能不能只用一套切圖就搞定所有解析度?」我認為這個話題值得單獨寫一篇文章好好探討,於是便有了
BaseAdapter 使用筆記:getView 的三種寫法與 ViewHolder 優化
適配器模式的應用價值在於兩點:1. 降低程式耦合性2. 容易擴展ListView 的顯示與緩存機制可以一句話概括:需要時才顯示,顯示完就進入緩存。而 BaseAdapte
Android 自訂動態圓環圓形頭像控件
許多 App 都會使用圓形頭像,而搭配動態圓環更能增加視覺層次。本文說明如何以自訂 View 實作一個帶有動態光環的圓形頭像控件。 圓形頭像的基本原理 核心做法是將圖片
Android 在 EditText 中插入 QQ 表情圖像
本文示範如何讓 EditText 除了文字之外,還能插入表情圖片。 效果預覽 先把 QQ 表情圖片放進 res 下的 drawable-hdpi 資料夾: 布局檔