Android 允许代码在不应运行时运行的if语句
我不知道为什么在我的代码中会发生这种情况,就我所能看到的而言,这应该是好的,尽管我已经做了一整天了 我的问题是,我的计数在选中复选框时会增加,在取消选中复选框时会减少,最多允许4个选项,并警告您不能选择第5个选项Android 允许代码在不应运行时运行的if语句,android,eclipse,Android,Eclipse,我不知道为什么在我的代码中会发生这种情况,就我所能看到的而言,这应该是好的,尽管我已经做了一整天了 我的问题是,我的计数在选中复选框时会增加,在取消选中复选框时会减少,最多允许4个选项,并警告您不能选择第5个选项 选项1-3工作正常-我将变量输出到log cat,它可以正常地上下移动,但一旦我按下第四个选项,我按下第五个选项,它就允许在我说的premCount的地方运行,我想我可以看到以下几点: 首先,您正在将前置计数器初始化为0?这意味着它将允许:0、1、2、3、4这五项 秒,在else中减少
选项1-3工作正常-我将变量输出到log cat,它可以正常地上下移动,但一旦我按下第四个选项,我按下第五个选项,它就允许在我说的
premCount的地方运行,我想我可以看到以下几点:
首先,您正在将前置计数器
初始化为0?这意味着它将允许:0、1、2、3、4这五项
秒,在else
中减少premCounter
。你不应该这样做。如果已选中4项(premCounter>=4
),则如果已选中该复选框,则允许取消选中该复选框并减小计数器。如果尚未选中,则显示警报(就像现在尝试检查新项目一样)
所以基本上这应该是可行的:
public void handlePremCheckboxClick(View v)
{
CheckBox tmpChkBox = (CheckBox) findViewById(v.getId());
if (premCounter < 4) {
if (tmpChkBox.isChecked()) {
tmpChkBox.setBackgroundColor(Color.parseColor("#ff33b5e5"));
premCounter++;
}
if (tmpChkBox.isChecked() == false) {
tmpChkBox.setBackgroundColor(Color.TRANSPARENT);
premCounter--;
}
}
else { // 4 have already been checked, now only allow unchecking.
tmpChkBox.setChecked(false);
tmpChkBox.setBackgroundColor(Color.TRANSPARENT);
if (!tmpChkBox.isChecked()) {
premCounter--;
}
else {
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);
}
public void handlePremCheckBox单击(视图v)
{
复选框tmpChkBox=(复选框)findViewById(v.getId());
如果(前置计数器<4){
if(tmpChkBox.isChecked()){
tmpChkBox.setBackgroundColor(Color.parseColor(“#ff33b5e5”);
前置计数器++;
}
if(tmpChkBox.isChecked()==false){
tmpChkBox.setBackgroundColor(颜色.透明);
前置计数器--;
}
}
else{//4已被选中,现在只允许取消选中。
tmpChkBox.setChecked(假);
tmpChkBox.setBackgroundColor(颜色.透明);
如果(!tmpChkBox.isChecked()){
前置计数器--;
}
否则{
AlertDialog.Builder alert=新建AlertDialog.Builder(此);
alert.setTitle(“达到最大团队数”)
.setMessage(“您最多选择了4支英超球队”)
.setPositiveButton(“确定”,空)
.show();
}
}
系统输出打印项次(前置计数器);
}
更新:我在以前的版本中把支票打错了。解释仍然保持不变。}否则如果(premCounter>4){
…premCounter--;
?我不知道这是否是导致它的原因,但它看起来很可疑……无论如何,当所有其他操作都失败时,让logcat在多个实例中打印premCounter
的值。就像任何if
或else
块中的第一行代码一样,并且就在所有增量之后de>和减量
行。事实上,将警报
代码移动到打印前置计数器
值的同一区域,这样您就可以清楚地看到事件的顺序。感谢您的回复,我已经测试了您的代码第五次按时不允许复选框被选中,如果您在之后按任何一次,都不允许复选框被选中允许检查我在答案中打错了。更新了代码并测试了它。工作正常!感谢您的帮助,当您双击复选框时,它仍在检查,但成功解决了问题。我将发布我所做的(如果可以的话)
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
public void handlePremCheckboxClick(View v)
{
CheckBox tmpChkBox = (CheckBox) findViewById(v.getId());
if (premCounter < 4) {
if (tmpChkBox.isChecked()) {
tmpChkBox.setBackgroundColor(Color.parseColor("#ff33b5e5"));
premCounter++;
}
if (tmpChkBox.isChecked() == false) {
tmpChkBox.setBackgroundColor(Color.TRANSPARENT);
premCounter--;
}
}
else { // 4 have already been checked, now only allow unchecking.
tmpChkBox.setChecked(false);
tmpChkBox.setBackgroundColor(Color.TRANSPARENT);
if (!tmpChkBox.isChecked()) {
premCounter--;
}
else {
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);
}