0

アプリケーションに問題があります。チェックボックスを 2 つまでに制限したいのですが、方法がわかりません。4つのチェックボックスとボタンがあります。ボタンが押されたとき、チェックされているチェックボックスが 2 つしかない場合は何かを実行し、3 つ以上のチェックボックスがある場合は別のことを実行します。これが私のコードです:

     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {

             if(chk1.isChecked()){
                  counter++;}
             else{
                 counter--;
             }
              if(chk2.isChecked())

              { counter++;}
              else{
                     counter--;
                 }
              if(chk3.isChecked())
              { counter++; }
              else{
                     counter--;
                 }
              if(chk4.isChecked())
              {  counter++; }
              else{
                     counter--;
                 }

             if ( (chk1.isChecked() || chk2.isChecked() || chk3.isChecked() || chk4.isChecked()) && counter >2 ) {

                 Toast.makeText(StartingPoint.this, "boo",  Toast.LENGTH_LONG).show();
                } 
             else {
                 Toast.makeText(StartingPoint.this, "no boo", Toast.LENGTH_LONG).show();
                }

            }


    });
4

1 に答える 1

0

私があなたのことを正しく理解していれば、あなたはもうほぼ理解しています。

  • カウンターを宣言する
  • デクリメントは忘れて、インクリメントのみを使用してください
  • 最初の if/then ステートメントを使用して、チェックされたチェックボックスの数 ( の値counter)を確認します。
  • ネストされた if/then ステートメントを使用して、特定のチェックボックスに基づいてロジックを適用します

編集- これは簡単な例です。

        /*
         * This will check the number of CheckBoxes checked when your
         * button is pressed, and perform some logic consequently.
         * 
         * ALTERNATIVE (not shown here): 
         * If you wish to disable the button based on how many CheckBoxes
         * are checked, you should add an OnClickListener for each CheckBox
         * (I'm over-simplifying here on purpose). 
         * Each CheckBox click would increase the counter if checked, 
         * decrease it if not.
         * Each listener would decide whether or not to enable the button
         * after computing the counter's value.   
         */

        Button myButton = null; // TODO initialize properly
        // TODO initialize all CheckBoxes properly
        final CheckBox cb0 = null;
        final CheckBox cb1 = null;
        final CheckBox cb2 = null;
        // ... and so forth ...

        // Initializing the anonymous listener class attached to the Button
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // initializing the counter each click
                int counter = 0;
                // incrementing the counter for each CheckBox checked
                if (cb0.isChecked()) counter++;
                if (cb1.isChecked()) counter++;
                if (cb2.isChecked()) counter++;
                // ... and so forth ...

                // Verifying number of CheckBoxes checked...
                // ...up to 2 CheckBoxes checked
                if (counter < 3) {
                    // TODO logic depending on which CheckBox(es)
                }
                // ...more than 2 CheckBoxes checked
                else {
                    // TODO logic depending on which CheckBox(es) or anything else
                }
            }
        });
于 2013-05-18T11:23:40.877 に答える