Photo by Pathum Danthanarayana on Unsplash
[CheckBox]
在 Android 手機/平板的應用中,CheckBox 核取方塊元件的應用十分廣泛。CheckBox 核取方塊是一個具有多選功能的選單,預設情況下,CheckBox 顯示為一個方塊圖示,在圖示旁邊顯示說明性文字。與 RadioButton 的最大差別是 RadioButton 是單選,需要搭配 RadioGroup 使用,而 CheckBox 核取方塊是多選的,每一個核取方塊都提供「選中」和「不選中」兩種狀態。在 XML 佈局檔中添加 CheckBox 核取方塊的基本語法格式如下:<CheckBox android:屬性 = "值"> </CheckBox>CheckBox 間接繼承自 Button,可以直接使用 Button 支援的屬性和方法,不同的是比 Button 多了可選中的功能,常用的屬性如下:
| XML屬性 | 描述 |
|---|---|
| text | 設定核取方塊右方的文字。 |
| checked | 用於指定選中狀態,屬性值為 true 表示選中;false 表示取消選中,預設為 false。 |
如果要知道 CheckBox 是否被選中有兩種方式,一是為為每個 CheckBox 添加事件監聽器 setOnCheckedChangeListener,二是增加一個按鈕,點擊後對每個 CheckBox 進行判斷 isChecked()。以下使用第二個方法為範例,檢查核取方塊 CheckBox 是否被按下。
[程式範例]
(1)新建一個模組 Module,進入 res/layout 目錄下點選佈局檔 activity_main.xml。將預設的內容刪除,建立 1 個 TextView 存放詢問內容,4 個核取方塊以及 1 個 Button ,以下是整個 XML 的內容。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_marginTop="50sp"
android:textSize="28sp"
android:text="問題:您喜愛的運動項目?" />
<CheckBox
android:id="@+id/ckb1"
android:text="游泳"
android:textSize="26sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/ckb2"
android:text="騎自行車"
android:textSize="26sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/ckb3"
android:text="慢跑"
android:textSize="26sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/ckb4"
android:text="打籃球"
android:textSize="26sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="送出"
android:textSize="24sp"
android:onClick="myonClickCKB"
android:layout_marginTop="30sp" >
</Button>
</LinearLayout>
(2)在 MainActivity.java 的 onCreate() 方法內,先建立與佈局檔的物件,用 findValueById 連結 CheckBox 元件,再建立一個程序 myonClickCKB ,這個程序是由XML 佈局檔中的 Button 定義的 onClick() 方法來呼叫的。使用 isChecked() 檢查 CheckBox 是否被按下,再使用 getText() 取得 CheckBox 的文字,並將這些文字合併成一個字串,透過 Toast 顯示在畫面上。程式碼如下:
package com.example.mycheckbox;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.Toast;
public class cb_MainActivity extends AppCompatActivity {
CheckBox ckb1, ckb2, ckb3, ckb4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cb_main);
//全螢幕
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ckb1 = (CheckBox) findViewById(R.id.ckb1); //透過ID取得Layout中的複選框1~4
ckb2 = (CheckBox) findViewById(R.id.ckb2);
ckb3 = (CheckBox) findViewById(R.id.ckb3);
ckb4 = (CheckBox) findViewById(R.id.ckb4);
}
public void myonClickCKB(View view) {
String msg = "您的運動愛好是:"; //初始化字串
if (ckb1.isChecked()) { //判斷複選框1是否被選定
msg = msg + ckb1.getText().toString(); //若選定,則將字串加該項目
}
if (ckb2.isChecked()) {
msg = msg + "、" + ckb2.getText().toString();
}
if (ckb3.isChecked()) {
msg = msg + "、" + ckb3.getText().toString();
}
if (ckb4.isChecked()) {
msg = msg + "、" + ckb4.getText().toString();
}
if (msg.equals("")) {
Toast.makeText(this, "請選擇您喜愛的運動項目", Toast.LENGTH_LONG).show(); // 顯示訊息
} else {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
}
執行的結果如下:[參考資料]
- Developer : CheckBox
- 明日科技:Android 從入門到精通


張貼留言