概要
チェックボックスの配列を出力します。「チェックしたアイテムを検索」ボタンを押すとユーザがチェックをしたアイテムの番号を出力し,「チェックボックスをクリア」ボタンを押すとユーザがチェックしたアイテムのチェックボックスをクリアします。checkBoxクラスの配列を利用します。
サンプルプログラム
package net.trusted_design.checkBoxArray;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
public class TDAndroid extends Activity
implements View.OnClickListener
{
private CheckBox[] checkBox; // チェックボックスの配列
int arrayCount = 5; // 配列の要素数
private Button checkButton; // チェックしたアイテムを検索するボタン
private Button clearButton; // チェックしたアイテムをクリアするボタン
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// レイアウトの作成
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.rgb(255,255,255));
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
// チェックボックス配列の用意
checkBox = new CheckBox[arrayCount]; // チェックボックス配列
for (int i=0; i<arrayCount; i++){
checkBox[i] = new CheckBox(this);
checkBox[i].setText("アイテム"+i);
checkBox[i].setTextColor(Color.rgb(0,0,0));
checkBox[i].setTextSize(16);
setMyLayoutParams(checkBox[i]);
layout.addView(this.checkBox[i]);
}
// ボタンの用意(checkButtonとclearButton)
checkButton = new Button(this);
checkButton.setText("チェックしたアイテムを検索");
checkButton.setOnClickListener(this);
setMyLayoutParams(checkButton);
layout.addView(checkButton);
clearButton = new Button(this);
clearButton.setText("チェックボックスをクリア");
clearButton.setOnClickListener(this);
setMyLayoutParams(clearButton);
layout.addView(clearButton);
}
// ボタンクリックイベントの処理(checkButtonまたはclearButtonが押される)
public void onClick(View view) {
// チェックボックスがチェックしてあるアイテムを検索する。
// チェック状態を保存する配列
boolean[] checkArray = new boolean[arrayCount];
// チェック数を保持
int checkCount = 0;
// チェックされていたら,checkArrayの対応要素がTrueになる。
for (int i=0; i<arrayCount; i++){
checkArray[i] = false;
checkArray[i] = checkBox[i].isChecked();
}
if (view == checkButton) { // checkButtonを押した
// チェックされているアイテムを出力する
String str = "";
for (int i=0; i<arrayCount; i++) {
if (true == checkArray[i]) {
str += "アイテム" + i + " ";
checkCount++;
}
}
if (0 != checkCount){
// 1つ以上のアイテムがチェックされている
str += "がチェックされています。";
} else {
// チェック済みアイテムなし
str = "チェックされているアイテムはありません。";
}
showDialog(this, "", str);
} else if (view == clearButton) {
// clearButtonを押した。
for (int i=0; i<arrayCount; i++) {
// チェックされているアイテムはチェックを外す
if (true == checkArray[i]) {
checkBox[i].setChecked(false);
}
}
} else {
showDialog(this, "", "エラー発生!");
}
}
// ダイアログの表示
private static void showDialog(final Activity activity,
String title, String text) {
AlertDialog.Builder ad = new AlertDialog.Builder(activity);
ad.setTitle(title);
ad.setMessage(text);
ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
activity.setResult(Activity.RESULT_OK);
}
});
ad.create();
ad.show();
}
// レイアウトのパラメータを設定する
private static void setMyLayoutParams(View view) {
view.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
実行結果
サンプルプログラムを動作させると,次のように動作します。
(1) サンプルプログラムを起動すると,チェックボックスが5つと,ボタンが2つ並んだ画面が出力されます。
適当にチェックボックスにチェックを入れて,「チェックしたアイテムを検索」ボタンまたは,「チェックボックスをクリア」ボタンを押してください。

(2) 「チェックしたアイテムを検索」ボタンを押すと,チェックされたアイテムの要素をダイアログで出力します。

(3) 「チェックボックスをクリア」ボタンを押すと,チェックボックスのチェック状態がクリアされます。

