Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 2.2 r1 API 中文(10):CheckBox 核取方塊

編輯:Android 開發實例

CheckBox 是複選的雙狀態控制項,使用者可以自由勾選或取消勾選,彼此之間不互斥。

類別結構

public class CheckBox extends CompoundButton

繼承鏈:Object → View → TextView → Button → CompoundButton → CheckBox

類別說明

它是一種特殊型態的雙狀態按鈕,共用 CompoundButton 提供的 setChecked()、isChecked() 與 OnCheckedChangeListener。由於每個 CheckBox 各自獨立,同一個畫面上放多個也不會互相影響,很適合「多選一組條件」的情境,例如通知設定、興趣勾選。

基本用法

<CheckBox android:id="@+id/checkbox_meat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我同意接收通知" />
CheckBox cb = (CheckBox) findViewById(R.id.checkbox_meat);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // isChecked 為 true 代表已勾選
    }
});

與 RadioButton 的選擇建議

需要「可以多選、且能隨時反悔」時用 CheckBox;需要「只能選一個」時改用 RadioButton 並包在 RadioGroup 裡。兩者的外觀與事件模型相近,差別主要在於互斥與否。

資料來源:Android 官方 API 文件整理(Android 2.2 r1)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved