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

Android 教學:RadioGroup 與 CheckBox 的 OnCheckedChangeListener 事件

編輯:Android 技術基礎

單選按鈕 RadioGroup 和複選框 CheckBox 都各自有 OnCheckedChangeListener 事件,但兩者並不是同一個介面。本章用同一個畫面示範兩種寫法,並把容易踩到的差異說清楚。

一、版面配置

打開 res/layout/activity_main.xml,放一組 RadioGroup(含男女兩個選項)與兩個 CheckBox。

<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true">

        <RadioButton
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男" />

        <RadioButton
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>

    <CheckBox
        android:id="@+id/football"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/gender"
        android:text="足球" />

    <CheckBox
        android:id="@+id/basketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/football"
        android:text="藍球" />

</RelativeLayout>

二、事件程式碼

打開 src/com.genwoxue.oncheckedchanged/MainActivity.java。

package com.genwoxue.oncheckedchanged;

import android.os.Bundle;
import android.app.Activity;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup.OnCheckedChangeListener;  // RadioGroup 專用的事件介面
import android.widget.CheckBox;
import android.widget.CompoundButton;                     // CheckBox 走這個父類別的介面
import android.widget.Toast;

public class MainActivity extends Activity {

    private RadioGroup GenderGroup = null;
    private RadioButton rbMale = null;
    private RadioButton rbFemale = null;
    private CheckBox cbFootBall = null;
    private CheckBox cbBasketBall = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GenderGroup = (RadioGroup) super.findViewById(R.id.gender);
        rbMale = (RadioButton) super.findViewById(R.id.male);
        rbFemale = (RadioButton) super.findViewById(R.id.female);
        cbFootBall = (CheckBox) super.findViewById(R.id.football);
        cbBasketBall = (CheckBox) super.findViewById(R.id.basketball);

        GenderGroup.setOnCheckedChangeListener(new GenderOnCheckedChangeListener());
        cbFootBall.setOnCheckedChangeListener(new BootBallOnCheckedChangeListener());
        cbBasketBall.setOnCheckedChangeListener(new BasketBallOnCheckedChangeListener());
    }

    // RadioGroup 的監聽器:回呼帶的是「被選中的 view id」
    private class GenderOnCheckedChangeListener implements OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            String sGender = "";
            if (rbFemale.getId() == checkedId) {
                sGender = rbFemale.getText().toString();
            }
            if (rbMale.getId() == checkedId) {
                sGender = rbMale.getText().toString();
            }
            Toast.makeText(getApplicationContext(), "您選擇的性別是:" + sGender, Toast.LENGTH_LONG).show();
        }
    }

    // CheckBox 的監聽器:回呼帶的是「是否勾選」的布林值
    private class BootBallOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton button, boolean isChecked) {
            String sFav = cbFootBall.getText().toString();
            sFav = isChecked ? sFav + "選中!" : sFav + "未選中";
            Toast.makeText(getApplicationContext(), "您選擇的愛好是:" + sFav, Toast.LENGTH_LONG).show();
        }
    }

    private class BasketBallOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton button, boolean isChecked) {
            String sFav = cbBasketBall.getText().toString();
            sFav = isChecked ? sFav + "選中!" : sFav + "未選中";
            Toast.makeText(getApplicationContext(), "您選擇的愛好是:" + sFav, Toast.LENGTH_LONG).show();
        }
    }
}

兩者的關鍵差別

RadioGroup 用的是 RadioGroup.OnCheckedChangeListener,回呼參數是「群組 + 被選中項目的 id」,適合判斷整組的變化。

CheckBox 則繼承自 CompoundButton,用的是 CompoundButton.OnCheckedChangeListener,回呼參數是「按鈕本身 + 布林狀態」,每個 CheckBox 都要各自註冊。這也是為什麼同樣叫 OnCheckedChange,卻不能共用同一個寫法。

三、執行結果

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