編輯:關於 Android 程式設計
表單類需求裡,單選與多選是最基礎的兩種互動。Android 提供了 RadioGroup+RadioButton 的組合來處理「只能選一個」,以及 CheckBox 來處理「可以選多個」。本文用一個性別選擇+興趣多選的小範例把兩者講清楚。
要點是:單選的互斥性由 RadioGroup 保證,而不是 RadioButton 自己。同一個 RadioGroup 內的多個 RadioButton 會自動互斥,不同 RadioGroup 之間則互不影響。監聽器建議掛在 RadioGroup 上(setOnCheckedChangeListener),而不是每個 RadioButton 各掛一個,這樣程式碼乾淨得多。
CheckBox 天生就是獨立的,彼此不影響。每個 CheckBox 各自實作 CompoundButton.OnCheckedChangeListener,在 onCheckedChanged 的 isChecked 參數中就能知道當前狀態。
package com.example.radiotest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private RadioGroup generGroup = null;
private RadioButton maleButton = null;
private RadioButton femaleButton = null;
private CheckBox swin = null, run = null, jump = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
generGroup = (RadioGroup) findViewById(R.id.mySexGroup);
maleButton = (RadioButton) findViewById(R.id.maleButton);
femaleButton = (RadioButton) findViewById(R.id.femaleButton);
swin = (CheckBox) findViewById(R.id.swinBox);
run = (CheckBox) findViewById(R.id.runBox);
jump = (CheckBox) findViewById(R.id.jumpBox);
// RadioGroup 的監聽器:一次處理整組的變化
generGroup.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == maleButton.getId()) {
Toast.makeText(MainActivity.this, R.string.male,
Toast.LENGTH_SHORT).show();
} else if (checkedId == femaleButton.getId()) {
Toast.makeText(MainActivity.this, R.string.female,
Toast.LENGTH_SHORT).show();
}
}
});
// CheckBox 各自監聽
swin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
System.out.println("swin is " + (isChecked ? "checked!" : "unchecked!"));
}
});
run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
System.out.println("run is " + (isChecked ? "checked!" : "unchecked!"));
}
});
jump.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
System.out.println("jump is " + (isChecked ? "checked!" : "unchecked!"));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RadioGroup
android:id="@+id/mySexGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male" />
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female" />
</RadioGroup>
<CheckBox
android:id="@+id/swinBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swin" />
<CheckBox
android:id="@+id/runBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run" />
<CheckBox
android:id="@+id/jumpBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jump" />
</LinearLayout>
不要用文字當 Toast 參數。範例原本寫成 Toast.makeText(this, male, ...) 是編譯不過的,第二個參數需要是 CharSequence 或資源 ID,字串資源請寫成 R.string.male。
監聽器要小心回收。在 ListView 的 convertView 中給 CheckBox 掛監聽器時,記得先把監聽器設為 null 再呼叫 setChecked,否則會觸發不必要的回呼並造成狀態錯亂。
預設選中。布局中加上 android:checked="true" 即可預設勾選;若用程式設定,同樣要注意監聽器時機。
樣式客製。系統預設的單選/多選圖示可以用 android:button 或 android:drawableLeft 換成自訂的 selector,讓表單風格與整體設計一致。
用 NineOldAndroids 打造可滑動刪除的 ListView 互動效果
在 Android 應用裡,列表項目的左右滑動刪除是一種常見又實用的互動方式。相比單純的點擊刪除,滑動手勢能讓用戶更直觀地管理列表內容。本文以 NineOldAndroi
Activity 與 Fragment 之間的資料交換(一):三種溝通方式與動畫切換
為什麼要用 Fragment?最大的好處是在單一 Activity 中就能呈現多種畫面,而且 Fragment 之間的切換動畫變化遠比 Activity 之間豐富。實務上
Android 仿淘寶商品詳情:拖動切換與標題欄漸變
效果目標 淘寶商品詳情頁有幾個很典型的互動:標題欄會隨滾動漸變,通知欄可透明處理,內容區可以繼續往下拖動並切換到詳情區。這種效果能讓商品資訊與詳細描述之間形成自然銜接。
解決 Eclipse 3.7 沒有自動提示代碼的方法
把 Android SDK 升級到最新版本後,由於 ADT 外掛需要 Eclipse 3.6 以上才能安裝,於是只好下載了 Eclipse 3.7。一切就緒後卻發現,撰寫