首页 > 解决方案 > 在android studio中编辑年龄文本中的验证

问题描述

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

我有验证问题我尝试了很多方法,但没有一个有效

我有编辑文本我想放置验证,只允许输入 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

标签: javaandroidandroid-studiovalidation

解决方案


我固定 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");

推荐阅读