安卓工作室中年龄编辑文本的验证



你好,我希望你们都做得好

我在验证方面有问题,我尝试了很多方法,但都不起作用

我有编辑文本,我想放验证,只允许输入从1到15的数字,但它不起任何帮助,请

String pet_name=petname.getText().toString();
String pet_type=ptype.getText().toString();
String pet_age=page.getText().toString();
String pet_gender=pgender.getText().toString();
String pet_location=plocation.getText().toString();
String pet_des=pdes.getText().toString();
String pet_img=petimg.getText().toString();
int inputInt = Integer.parseInt(page.getText().toString());

if(pet_name.equals(""))
petname.setError("User name Should not be empty");
else if(!pet_name.matches(namevalid))
petname.setError("Should be Only letters");
else if(pet_type.equals(""))
ptype.setError("Should not be empty");
else if(!pet_type.startsWith("Cat")&& !pet_type.startsWith("Dog"))
ptype.setError("Should be Only Cat or Dog");
else if(pet_age.equals(""))
page.setError("Should not be empty");
else if (inputInt <= 15 && inputInt >= 1 ) ;
page.setError("Should be be between 1 to 15 year");

当我添加这个代码程序时,问题就出现了

else if (inputInt <= 15 && inputInt >= 1 ) ;
page.setError("Should be be between 1 to 15 year");

enter code here

i将thx固定到所有

当我添加年龄代码时,年龄下的其他验证停止工作,所以我把所有代码都放在年龄验证中,现在它正在工作

else if(!page.getText().toString().isEmpty()) {
//check to see if the entered donation is superior to 5 if so go to make payment page
int inputInt = Integer.parseInt(page.getText().toString());
if (inputInt <= 10 && inputInt >= 1) {
if(pet_gender.equals(""))
pgender.setError("Should not be empty");
else if(!pet_gender.startsWith("Male")&& !pet_gender.startsWith("Female"))
pgender.setError("Should be Only Male or Female");
else if(pet_location.equals(""))
plocation.setError("Should not be empty");
else if(pet_des.equals(""))
pdes.setError("Should not be empty");
else if(pet_img.equals(""))
petimg.setError("Should not be empty");

您的情况与类似

else if (inputInt >= 10 && inputInt <= 1 )

在上述情况下,您正在使用&amp;(AND(运算符。意味着两个条件都应为真(左一个和右一个(。正如你所知,这两者都不会是真的。(inputInt的值不会同时>10和<1(

所以您需要||(OR(运算符。你的情况会像这个

else if (inputInt < 1 || inputInt > 15 ) 
page.setError("Should be be between 1 to 15 year");

我相信你是有意这么做的?

else if (inputInt >= 15 || inputInt <= 1 ) ;
page.setError("Should be be between 1 to 15 year");

请展开您收到的错误是什么,

为了更好地编码,请尝试更改:

int inputInt = Integer.parseInt(page.getText().toString());

至:

int inputInt = Integer.parseInt(pet_age);

因为您已经在页面的编辑文本中收到了文本,所以再次拖动它将是重复和不必要的。

else if(!page.getText().toString().isEmpty()) {
//check to see if the entered donation is superior to 5 if so go to make payment page
int inputInt = Integer.parseInt(page.getText().toString());
if (inputInt <= 10 && inputInt >= 1) {
if(pet_gender.equals(""))
pgender.setError("Should not be empty");
else if(!pet_gender.startsWith("Male")&& !pet_gender.startsWith("Female"))
pgender.setError("Should be Only Male or Female");
else if(pet_location.equals(""))
plocation.setError("Should not be empty");
else if(pet_des.equals(""))
pdes.setError("Should not be empty");
else if(pet_img.equals(""))
petimg.setError("Should not be empty");``

最新更新