【问题标题】:validation in edit text of age in android studio在android studio中编辑年龄文本中的验证
【发布时间】:2021-04-27 06:50:57
【问题描述】:

你好,希望你们一切都好

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

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

【问题讨论】:

  • 条件应该是 else if (inputInt >= 16 || inputInt

标签: java android android-studio validation


【解决方案1】:

你的情况是这样的

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

在上述情况下,您正在使用 &&(AND) 运算符。表示条件都应该为真(左一个和右一个)。正如您也知道的那样,这两者永远不会正确。(inputInt 的值不会同时大于 10 和

所以你需要 ||(OR) 运算符。你的情况应该是这样的

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

【讨论】:

  • 不会让 inputInt 大于 15 且小于 1。用户想要介于 1 和 15 之间的数字
  • @lyncx 这是为了验证。表示当 inputInt 大于 15 或小于 1 时,OP 可以显示数字应在 1 到 15 范围内的消息。此条件将检测数字是否不在给定范围内
  • 对,我错过了。
【解决方案2】:

我相信你是故意的?

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

【讨论】:

  • 是的,我写错了代码......但我仍然有错误:(
【解决方案3】:

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

为了更好的编码,尝试改变:

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

到:

int inputInt = Integer.parseInt(pet_age);

因为你已经收到了页面编辑文本里面的文字,所以再拉一遍是重复的,没必要的。

【讨论】:

    【解决方案4】:

    我已修复所有问题

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

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

    【讨论】:

      【解决方案5】:
      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");``
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-10
        • 2014-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多