我希望在xml中定义的单个onClick函数中存在多个toast。
public void confirm(View v) {
Name = text1.getText().toString();
age = text2.getText().toString();
phonenumber = text3.getText().toString();
gender="";
if(r1.isChecked())
gender="MALE";
else if(r2.isChecked())
gender="FEMALE";
else
Toast.makeText(getApplicationContext(),"Select Gender",Toast.LENGTH_LONG).show();
if (Name.length()==0)
Toast.makeText(getApplicationContext(),"Name Is A Mandatory Field",Toast.LENGTH_LONG).show();
if(age.length()>3 || age.length()==0)
Toast.makeText(getApplicationContext(),"Enter Correct Age",Toast.LENGTH_LONG).show();
if(age.length()>0 && Name.length()>0 && gender.length()>0 && phonenumber.length()>0)
Toast.makeText(getApplicationContext(), "OTP Sent", Toast.LENGTH_LONG).show();
}
}
这似乎不起作用。只有性别工作。其他人根本不工作。
答案 0 :(得分:0)
这将捕获先出现的错误。我建议按照UI中输入字段的顺序对它们进行排序。
if(!r1.isChecked && !r2.isChecked)
Toast.makeText(getApplicationContext(),"Select Gender",Toast.LENGTH_LONG).show();
else if (Name.length()==0)
Toast.makeText(getApplicationContext(),"Name Is A Mandatory Field",Toast.LENGTH_LONG).show();
else if(age.length()>3 || age.length()==0)
Toast.makeText(getApplicationContext(),"Enter Correct Age",Toast.LENGTH_LONG).show();
else if(age.length()>0 && Name.length()>0 && gender.length()>0 && phonenumber.length()>0)
Toast.makeText(getApplicationContext(), "OTP Sent", Toast.LENGTH_LONG).show();
else if(r1.isChecked())
gender="MALE";
else if(r2.isChecked())
gender="FEMALE";
这不是验证用户输入的理想方式。让多个吐司一个接一个地出现并不是一个好主意。在网站中,如果用户错过了多个字段,您可以输入自定义通知,请输入所有字段,或者只是将错过的第一个字段的通知设置好,然后在完成后,将显示下一个丢失的字段通知。 / p>
答案 1 :(得分:-1)
使用if(Name.trim()。equals(“”))而不是if(Name.length == 0)
public void confirm(View v) {
Name = text1.getText().toString();
age = text2.getText().toString();
phonenumber = text3.getText().toString();
gender="";
if(r1.isChecked())
gender="MALE";
else if(r2.isChecked())
gender="FEMALE";
else
Toast.makeText(getApplicationContext(),"Select Gender",Toast.LENGTH_LONG).show();
**if (Name.trim().equals(""))**
Toast.makeText(getApplicationContext(),"Name Is A Mandatory Field",Toast.LENGTH_LONG).show();
**if(age.length()>3 || age.trim().equals(""))**
Toast.makeText(getApplicationContext(),"Enter Correct Age",Toast.LENGTH_LONG).show();
if(age.length()>0 && Name.length()>0 && gender.length()>0 && phonenumber.length()>0)
Toast.makeText(getApplicationContext(), "OTP Sent", Toast.LENGTH_LONG).show();
}