私は一日中これに取り組んでいましたが、私が見ることができる限り、これが私のコードでなぜ起こっているのか分かりません。
私の問題は、チェックボックスを選択すると増加するか、チェックを外すと減少するカウントがあり、最大4つの選択肢が許可され、5番目の選択肢を選択できないという警告が表示されることです。
選択肢 1 ~ 3 は正常に動作します。変数を log cat に出力すると、正常に上下しますpremCount<=4
。
アラートは 6 回目のプレスで表示されますが、ユーザーはそれを閉じてから再度選択することができます。
これが私のコードです:
public void handlePremCheckboxClick(View v){
CheckBox tmpChkBox = (CheckBox) findViewById(v.getId());
if (premCounter<=4){ <---this part is allowing 5, 6, 7, etc presses through
if(tmpChkBox.isChecked())
{
tmpChkBox.setBackgroundColor(Color.parseColor("#ff33b5e5"));
premCounter++;
}
if(tmpChkBox.isChecked()== false)
{
tmpChkBox.setBackgroundColor(Color.TRANSPARENT);
premCounter--;
}
}else if(premCounter >4) {
tmpChkBox.setChecked(false);
tmpChkBox.setBackgroundColor(Color.TRANSPARENT);
premCounter--;
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Max Teams Reached")
.setMessage("You have selected the max of 4 Premiership teams")
.setPositiveButton("OK", null)
.show();
}
System.out.println(premCounter);
}
ログキャット:
10-18 17:10:58.859: D/OpenGLRenderer(25923): Enabling debug mode 0
10-18 17:11:06.146: D/dalvikvm(25923): GC_FOR_ALLOC freed 125K, 2% free 9286K/9448K, paused 17ms, total 17ms
10-18 17:11:06.377: W/IInputConnectionWrapper(25923): finishComposingText on inactive InputConnection
10-18 17:11:07.258: I/System.out(25923): 1
10-18 17:11:07.488: I/System.out(25923): 2
10-18 17:11:07.748: I/System.out(25923): 3
10-18 17:11:08.039: I/System.out(25923): 2
10-18 17:11:08.689: I/System.out(25923): 3
10-18 17:11:08.939: I/System.out(25923): 4
10-18 17:11:09.240: I/System.out(25923): 5
10-18 17:11:09.550: I/System.out(25923): 4
10-18 17:11:10.461: I/System.out(25923): 5
10-18 17:11:10.801: I/System.out(25923): 4
10-18 17:11:11.843: I/System.out(25923): 5
10-18 17:11:12.483: I/System.out(25923): 4
10-18 17:11:13.444: I/System.out(25923): 5
10-18 17:11:13.695: I/System.out(25923): 4
10-18 17:11:14.706: I/System.out(25923): 5
10-18 17:11:15.086: I/System.out(25923): 4
10-18 17:11:16.217: I/System.out(25923): 5