編輯:關於 Android 開發
單選與多選是表單裡最常見的兩種互動,Android 分別用 RadioButton 與 CheckBox 來實作。這篇把基本用法、取值方式與樣式自訂一次講完。
RadioButton 必須放在 RadioGroup 裡,這樣同一組內才會互斥,只能選一個。RadioGroup 上的 orientation 屬性可以控制排列方向。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請選擇你的性別"
android:textSize="30sp"/>
<RadioGroup
android:id="@+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"/>
</LinearLayout>
取得選中值有兩種做法。
做法 A:監聽 RadioGroup 的選擇變化(推薦,即時反應)
public class MainActivity extends Activity {
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.rg1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
RadioButton radioButton = (RadioButton) findViewById(checkId);
Toast.makeText(MainActivity.this,
"你選中了" + radioButton.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
}
}
做法 B:在按下按鈕時遍歷取值(適合送出表單的時機)
public class MainActivity extends Activity {
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.rg1);
Button btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
if (radioButton.isChecked()) {
Toast.makeText(MainActivity.this,
"你選擇了" + radioButton.getText(),
Toast.LENGTH_LONG).show();
break;
}
}
}
});
}
}
CheckBox 的用法幾乎與 RadioButton 相同,差別只在於它允許複選,所以不放在 RadioGroup 裡。
<CheckBox android:id="@+id/cb1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="蘋果"/>
<CheckBox android:id="@+id/cb2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="香蕉"/>
<CheckBox android:id="@+id/cb3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="梨子"/>
複選的取值要逐一檢查 isChecked(),把勾選的項目串起來:
public class MainActivity extends Activity
implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
private CheckBox checkBox1, checkBox2, checkBox3;
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox1 = (CheckBox) findViewById(R.id.cb1);
checkBox2 = (CheckBox) findViewById(R.id.cb2);
checkBox3 = (CheckBox) findViewById(R.id.cb3);
btn1 = (Button) findViewById(R.id.btn1);
checkBox1.setOnCheckedChangeListener(this);
checkBox2.setOnCheckedChangeListener(this);
checkBox3.setOnCheckedChangeListener(this);
btn1.setOnClickListener(this);
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()) {
Toast.makeText(MainActivity.this,
"你選中了" + compoundButton.getText(),
Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View view) {
String choose = "";
if (checkBox1.isChecked()) choose += checkBox1.getText().toString();
if (checkBox2.isChecked()) choose += checkBox2.getText().toString();
if (checkBox3.isChecked()) choose += checkBox3.getText().toString();
Toast.makeText(MainActivity.this, "你選中了" + choose, Toast.LENGTH_LONG).show();
}
}
系統預設的方框在新版 Android 上未必符合設計稿。以 CheckBox 為例,有兩種做法,本質相同,都是換掉 drawable。
做法一:StateListDrawable + button 屬性
先定義一個 selector,用 state_checked 區分勾選與未勾選的圖:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_enabled="true"
android:drawable="@mipmap/btn_radio_on"/>
<item android:state_checked="false" android:state_enabled="true"
android:drawable="@mipmap/btn_radio_off"/>
</selector>
再在版面檔中用 android:button 指到這個 selector 即可。
做法二:自訂 style
第一步同樣先寫好 selector;第二步在 styles.xml 定義一個樣式,把 android:button 指過去,並一併設定文字大小、間距等屬性;第三步在版面檔中用 style="@style/你的樣式名稱" 套用。這種做法的好處是樣式可重複使用,多個畫面共用同一套視覺規格時特別省事。
兩種做法都能達到相同效果,差別只在於是否要抽成可重複使用的樣式。實際效果如下圖所示,有興趣可以自己動手試試每一個屬性的變化。
Android LinearLayout 常用布局實戰:垂直與嵌套對齊
用 LinearLayout 實現常用的對齊布局 下面要實現的是一種頗為常用的布局效果:外層垂直排列,內部通過嵌套 LinearLayout 和 gravity 屬性,實
Android UI 組件深入:ImageButton 與 ZoomButton 圖文解析
透視 ImageButton 與 ZoomButton:讓圖片按鈕真正「活」起來 單看 ImageButton 這個名字,很容易以為它就是一個普通的圖片按鈕,把圖片往上一
深入解析 android:exported 屬性:四大組件的對外暴露機制
某天用 360 掃描應用漏洞時,掃描結果裡出現了一個 android:exported 屬性。老實說,在此之前我完全不知道這個屬性是什麼、又有何用處,於是決定查閱官方 A
在 ListView 或 RecyclerView 中顯示可新增圖片的實作
在 ListView 或 RecyclerView 中顯示可新增圖片的實作這個效果看似是加上傳功能,實際上可以把整個區塊做成 ListView 或 RecyclerVie