0

我需要你的帮助。我有几个复选框,我希望在未选中任何复选框时显示一条消息,通知用户在 Toast 视图中选择至少 1 个复选框。这是我的代码,现在如果没有选中复选框,它会显示消息,但之后会显示另一条消息。

if(ketch.isChecked()==true){

                    result2.append("Ketchup"+"\n");
                }
                if(may.isChecked()==true){

                    result2.append("Mayonnaise"+"\n");
                }
                if(mus.isChecked()==true){

                    result2.append("Mustard"+"\n");
                }
                else{


                    Toast.makeText(MainActivity.this,"Please choose at least 1 topping!",Toast.LENGTH_SHORT).show();

                }




Toast.makeText(MainActivity.this, "Name: "+name.getText()+"\n"+"Phone: "+phone.getText()+"\n"+"Address: " +address.getText()+"\n"+"Shape: "+sh.getText()+"\n"+"Cheese: "+ty.getText()+"\n"+"Toppings: "+"\n"+result.toString()+"Sauce: "+"\n"+result2.toString(), Toast.LENGTH_LONG).show();


        }
        });

    }
4

5 回答 5

1

我认为您可以创建 List checkboxesList 并将所有 CheckBoxes 放在那里,然后您可以像这样创建您的方法:

public boolean isAnyCheckboxChecked() {
    for(Checkbox chekbox : checkboxesList)
       if(checkbox.isChecked())
          return true;
    return false;
}

这可能是做到这一点的最佳方式。

于 2013-09-27T10:34:37.050 回答
1

使用 else if

if(ketch.isChecked()){

                result2.append("Ketchup"+"\n");
            }
            else if(may.isChecked()){

                result2.append("Mayonnaise"+"\n");
            }
            else if(mus.isChecked()){

                result2.append("Mustard"+"\n");
            }
            else{


                Toast.makeText(MainActivity.this,"Please choose at least 1 topping!",Toast.LENGTH_SHORT).show();

            }


            Toast.makeText(MainActivity.this, "Name: "+name.getText()+"\n"+"Phone: "+phone.getText()+"\n"+"Address: " +address.getText()+"\n"+"Shape: "+sh.getText()+"\n"+"Cheese: "+ty.getText()+"\n"+"Toppings: "+"\n"+result.toString()+"Sauce: "+"\n"+result2.toString(), Toast.LENGTH_LONG).show();
        }
    });

}

纯java!

于 2013-09-27T10:03:17.010 回答
1

试试看:

boolean atLeastOneChecked = false;

if(ketch.isChecked()==true){

    result2.append("Ketchup"+"\n");
    atLeastOneChecked = true;
}
if(may.isChecked()==true){

    result2.append("Mayonnaise"+"\n");
    atLeastOneChecked = true;
}
if(mus.isChecked()==true){

    result2.append("Mustard"+"\n");
    atLeastOneChecked = true;
}

if (!atLeastOneChecked)
    Toast.makeText(MainActivity.this,"Please choose at least 1 topping!",Toast.LENGTH_SHORT).show();
else 
    Toast.makeText(MainActivity.this, "Name: "+name.getText()+"\n"+"Phone: "+phone.getText()+"\n"+"Address: " +address.getText()+"\n"+"Shape: "+sh.getText()+"\n"+"Cheese: "+ty.getText()+"\n"+"Toppings: "+"\n"+result.toString()+"Sauce: "+"\n"+result2.toString(), Toast.LENGTH_LONG).show();
于 2013-09-27T10:04:55.600 回答
0
if(ketch.isChecked()==true){
    result2.append("Ketchup"+"\n");
}
if(may.isChecked()==true){
    result2.append("Mayonnaise"+"\n");
}
if(mus.isChecked()==true){
    result2.append("Mustard"+"\n");
}

if(!(ketch.isChecked() || may.isChecked() || mus.isChecked())){
     Toast.makeText(MainActivity.this,"Please choose at least 1 topping!",Toast.LENGTH_SHORT).show();
}else{
            Toast.makeText(MainActivity.this, "Name: "+name.getText()+"\n"+"Phone: "+phone.getText()+"\n"+"Address: " +address.getText()+"\n"+"Shape: "+sh.getText()+"\n"+"Cheese: "+ty.getText()+"\n"+"Toppings: "+"\n"+result.toString()+"Sauce: "+"\n"+result2.toString(), Toast.LENGTH_LONG).show();    
}
于 2013-09-27T10:18:59.567 回答
0

更清洁的实现方式

    if(ketch.isChecked())
    {
        result2.append("Ketchup"+"\n");
    }
    else if(may.isChecked())
    {
        result2.append("Mayonnaise"+"\n");
    }
    else if(mus.isChecked())
    {
        result2.append("Mustard"+"\n");
    }
    else
    {
        Toast.makeText(MainActivity.this,"Please choose at least 1 topping!",Toast.LENGTH_SHORT).show();
    }
    Toast.makeText(MainActivity.this, "Name: "+name.getText()+"\n"+"Phone: "+phone.getText()+"\n"+"Address: " +address.getText()+"\n"+"Shape: "+sh.getText()+"\n"+"Cheese: "+ty.getText()+"\n"+"Toppings: "+"\n"+result.toString()+"Sauce: "+"\n"+result2.toString(), Toast.LENGTH_LONG).show();
于 2013-09-27T10:05:06.083 回答