編輯:關於 Android 開發
在 Android 中,我們通常透過 RadioGroup 管理一組 RadioButton,以達到單選互斥的效果。但有些需求需要 N 行 N 列的 RadioButton 矩陣,而 RadioGroup 本質上是一個「耿直」的 LinearLayout,無法完成網格布局。本文就來分享一種以 GridView 實現網格化單選按鈕的思路。

提到網格布局,最簡單的做法就是使用系統的 GridView,把 RadioButton 放進去,再自行實作按鈕之間的互斥邏輯。
我們建立一個名為 IconRadioButton 的自訂元件:
public class IconRadioButton extends LinearLayout {
private boolean mIsCheck;
private ImageView mRadio;
private TextView mTextView;
private ColorStateList mTextColor;
public IconRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
View mContentView = LayoutInflater.from(context)
.inflate(R.layout.btn_icon_radio, this);
mRadio = (ImageView) mContentView.findViewById(R.id.radio);
mTextView = (TextView) mContentView.findViewById(R.id.text);
}
public void setChecked(boolean isChecked) {
mIsCheck = isChecked;
changeUIByChecked(isChecked);
}
public boolean isChecked() { return mIsCheck; }
private void changeUIByChecked(boolean isChecked) {
if (mRadio.getVisibility() != GONE) {
mRadio.setImageResource(isChecked
? R.drawable.radio_selected : R.drawable.radio_unselected);
}
mTextView.setEnabled(isChecked);
}
public void setTextColor(ColorStateList color) {
mTextColor = color;
mTextView.setTextColor(color);
}
// 省略 setText / hiddenRadio 等方法
}
這個元件本質上就是一個 LinearLayout,內部封裝了依據是否選中來切換文字與按鈕樣式的邏輯,布局中放了一個 ImageView 與一個 TextView。
public class RadioItemModel {
public String text; // RadioButton 的文字
public boolean isChecked; // 是否選中
public boolean hiddenRadio; // 是否隱藏 radio
public ColorStateList textColor; // 不同狀態下的文字顏色
public RadioItemModel(String text) {
this.isChecked = false;
this.hiddenRadio = false;
this.textColor = null;
this.text = text;
}
}
其中 ColorStateList 用來控制 TextView 在不同狀態(如 pressed)下的文字顏色。
互斥邏輯在 Adapter 中實現,重點方法包括:
public class GridRadioAdapter extends BaseAdapter {
private List<RadioItemModel> mList;
private OnItemCheckedListener mListener;
// 檢查是否有預設選中,若無則預設選中第一個
private void checkDefaultChecked() {
for (RadioItemModel item : mList) {
if (item.isChecked) return;
}
mList.get(0).isChecked = true;
}
// 檢查互斥:若多個被選中,只保留最後一個
private void checkMutex() {
int checkCount = 0;
for (RadioItemModel item : mList) {
if (item.isChecked) checkCount++;
}
if (checkCount >= 2) {
setOtherUnChecked(checkCount - 1);
}
}
// getView 中為每個 IconRadioButton 設定點擊事件,
// 點擊時呼叫 setOtherUnChecked(position) 並回呼 mListener
}
在 constructor 中會呼叫 check 來執行預設選中檢查與互斥檢查:若資料中沒有預設選中項,則讓第一個按鈕默認選中;若同時有多個被選中,則保留最後一個為選中狀態。此外 OnItemCheckedListener 為按鈕被選中時的回呼。
使用起來相當直覺:
private void initData() {
mItemList = new ArrayList<>();
ColorStateList csl = getResources().getColorStateList(R.color.ddt_color_tab_text);
mItemList.add(new RadioItemModel("全部", false, true, csl));
mItemList.add(new RadioItemModel("早餐前", false, true, csl));
mItemList.add(new RadioItemModel("午餐前", false, true, csl));
// ...其餘項目
mAdapter.notifyDataSetChanged();
}
PS:若把 hiddenRadio 設為 true,還能做出如下圖的切換效果,因此它也很適合拿來做切換 Fragment 的選項卡。

以上就是以 GridView 打造網格式 RadioButton 矩陣的完整思路,希望對你有所啟發。
Android 文字漸變與歌詞進度效果實現
文字漸變效果Android 可透過 LinearGradient 為文字加上線性漸變,也能利用自定義 View 或 ProgressBar 做出歌詞進度效果。Linear
Android 反射機制實現與原理:從 Java 反射到實際應用
反射的基本概念 反射是指程序在運行時能夠檢查、訪問和修改自身結構與行為的能力。在 Java 中,反射機制允許程序動態獲取類的信息,例如構造方法、成員方法、字段、父類、接口
Cordova(PhoneGap)筆記:透過 CordovaPlugin 插件調用 Activity
Cordova 透過 CordovaPlugin 插件調用 Activity 引言 Cordova(PhoneGap)採用 HTML5 + JavaScript 的混合模
Activity 四種啟動模式實測比較:standard、singleTop、singleTask、singleInstance
Activity 四種啟動模式實測比較:standard、singleTop、singleTask、singleInstance Activity 共有四種啟動模式:st