概要
チェックボックスの配列を出力します。「チェックしたアイテムを検索」ボタンを押すとユーザがチェックをしたアイテムの番号を出力し,「チェックボックスをクリア」ボタンを押すとユーザがチェックしたアイテムのチェックボックスをクリアします。checkBoxクラスの配列を利用します。
サンプルプログラム
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 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) 「チェックボックスをクリア」ボタンを押すと,チェックボックスのチェック状態がクリアされます。
