我希望在一个onClick函数中存在多个toast,这个函数是我在xml中定义的。
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();
}
}这似乎不是working.Only性别工作,.Others根本不工作。
发布于 2016-03-16 17:31:19
这将捕获最先出现的任何错误。我建议您在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";这不是验证用户输入的理想方法。让多个祝酒词一个接一个地出现并不是一个好主意。在一个网站中,如果用户错过了几个字段,你可以有一个自定义的通知,告诉你,请输入所有字段,或者只是发布第一个字段丢失的通知,然后当完成后,下一个缺少的字段通知将显示出来。
发布于 2016-03-16 05:33:54
使用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();
}https://stackoverflow.com/questions/36022403
复制相似问题