Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

CheckBox 復選框控件的動態添加與取值

編輯:關於 Android 程式設計

本文介紹 CheckBox 復選框控件的使用方法,重點示範如何用 Java 代碼動態地添加復選框並取得勾選結果。

一、簡介

類結構圖:

二、使用方法

這裡示範用 Java 代碼在 LinearLayout 中動態添加 CheckBox:

  1. 新建 LinearLayout 布局。
  2. 建立 CheckBox 的 XML 布局檔。
  3. 透過 View.inflate() 方法創建 CheckBox。
  4. 透過 LinearLayout 的 addView 方法添加。
  5. 透過 List<CheckBox> 完成輸出。
CheckBox checkBox = (CheckBox) View.inflate(this, R.layout.checkbox, null);
ll_checkBoxList.addView(checkBox);
for (CheckBox checkBox : checkBoxList) { ... }

三、程式碼實例

效果圖:

package fry;

import java.util.ArrayList;
import java.util.List;
import com.example.CheckBoxDemo1.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Activity01 extends Activity implements OnClickListener {
    private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();
    private LinearLayout ll_checkBoxList;
    private Button btn_ok;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity01);
        ll_checkBoxList = (LinearLayout) findViewById(R.id.ll_CheckBoxList);
        btn_ok = (Button) findViewById(R.id.btn_ok);

        String[] strArr = {"你是學生嗎?", "你是否喜歡 android", "您喜歡旅遊嗎?", "打算出國嗎?"};
        for (String str : strArr) {
            CheckBox checkBox = (CheckBox) View.inflate(this, R.layout.checkbox, null);
            checkBox.setText(str);
            ll_checkBoxList.addView(checkBox);
            checkBoxList.add(checkBox);
        }
        btn_ok.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String str = "";
        for (CheckBox checkBox : checkBoxList) {
            if (checkBox.isChecked()) {
                str += checkBox.getText().toString() + "\n";
            }
        }
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }
}

activity01.xml:

<?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" >

    <LinearLayout
        android:id="@+id/ll_CheckBoxList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

    <Button
        android:id="@+id/btn_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="確定" />
</LinearLayout>

checkbox.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</CheckBox>

四、重點整理

動態添加 CheckBox 的關鍵在於 View.inflate() 創建、addView 添加、以及用 List<CheckBox> 統一管理。最後遍歷列表、用 isChecked() 判斷勾選狀態即可取得所有被選中的項目。這種做法特別適合選項數量不固定的場景。

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