Android如果没有选中复选框

时间:2013-09-27 10:00:48

标签: android checkbox

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

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();


        }
        });

    }

5 个答案:

答案 0 :(得分:1)

如果

,请使用else
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();
        }
    });

}

Pure java!

答案 1 :(得分: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();

答案 2 :(得分:1)

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

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

这可能是最好的方法。

答案 3 :(得分: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();

答案 4 :(得分: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();    
}