【问题标题】:Check entered value is number or not检查输入的值是否为数字
【发布时间】:2013-07-25 01:42:24
【问题描述】:

这个sn-p我试过了,但是不行

try 
    {
    Integer.parseInt(enteredID.getText().toString());
    Log.i("enteredID value", "enterdID is numeric!!!!!!!!!!!^^^");
    flag=1;
} catch (NumberFormatException e) {
    flag=-1;
    Log.i("enteredID value", "enterdID isn't numeric!!!!!!!!!!!^^^");
}

注意它可以接受用户名或 id 来检查值, 我不希望它只接受数字!!

【问题讨论】:

    标签: android integer


    【解决方案1】:

    如果布尔值为真,则为数字,否则为字符串

    boolean digitsOnly = TextUtils.isDigitsOnly(editText.getText());
    

    或者举例

    String text = edt_email.getText().toString();
                boolean digitsOnly = TextUtils.isDigitsOnly(text);
                if (digitsOnly) {
                     if (text.length() == 0) {
                        Toast.makeText(getApplicationContext(), "field can't be empty.", Toast.LENGTH_LONG).show();
                     } else {
                       Toast.makeText(getApplicationContext(), "field is int value", Toast.LENGTH_LONG).show();
                     }
                }else {
                        Toast.makeText(getApplicationContext(), "Field is string value", Toast.LENGTH_LONG).show();
                    }
                }
    

    【讨论】:

      【解决方案2】:

      仅将此表达式用于验证编号

      String regexStr = "^[0-9]*$";
      
      if(et_number.getText().toString().trim().matches(regexStr))
      {
          //write code here for success
      }
      else{
          // write code for failure
      }
      

      【讨论】:

      • 这对我有用,除了我也试图允许小数点,当我复制像 33.8 这样的数字时,它失败了......我如何更新 regexStr 以允许小数点也?
      • 你必须用 "^[1-9]\d*(\.\d+)?$" 替换这个 "^[0-9]*$" 字符串
      • 为我工作。但在 android 中使用 "^[1-9]\\d*(\\.\\d+)?$" 谢谢
      【解决方案3】:

      设置 EditText 属性 inputType = number 它总是将数字作为输入

      android:inputType="number"
      

      【讨论】:

      • thnx,但我写了一个注释,我希望用户输入数字和字符,以便执行每段不同的代码
      【解决方案4】:

      试试这个正则表达式:

      String regex = "-?\\d+(\\.\\d+)?";
      
      if (enteredID.getText().toString().matches(regex))  {
           Log.i("enteredID value", "enterdID is numeric!!!!!!!!!!!^^^");
           flag=1;
       } else {
           flag=-1;
           Log.i("enteredID value", "enterdID isn't numeric!!!!!!!!!!!^^^");
       }
      

      【讨论】:

        【解决方案5】:

        为此使用TextUtils 类函数

        TextUtils.isDigitsOnly("gifg");
        

        如果只有数字则返回true,如果字符串中存在任何字符则返回false

        【讨论】:

          【解决方案6】:

          使用这个方法

           boolean isNumber(String string) {
              try {
                int amount = Integer.parseInt(string);
                return true;
              } catch (Exception e) {
                return false;
              }
            }
          

          像这样:

          if (isNumber(input)) {
            // string is int
          }
          

          【讨论】:

            【解决方案7】:

            你真的不应该以这种方式使用异常。当您期望像这样抛出异常时,您应该找到另一种处理方式。

            尝试使用:http://developer.android.com/reference/android/widget/TextView.html#setRawInputType%28int%29

            应该这样做:

            editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
            

            【讨论】:

              【解决方案8】:

              大概是这样的:

                  String text = enteredID.getText().toString();
              
                  if(text.matches("\\w+")){
                    //--words--
                  }else if (text.matches("\\d+")){
                    //--numeric--
                  }else {
                    //-- something else --
                  }
              

              您可以更改正则表达式以匹配复杂格式。

              【讨论】:

                【解决方案9】:
                Pattern ptrn = Pattern.compile(regexStr);
                
                if (!ptrn.matcher(et_number.getText().toString().trim()).matches()) {
                
                    //write code here for success
                
                }else{
                
                    //write code here for success
                
                }
                

                【讨论】:

                  【解决方案10】:
                  String text = editText123.getText().toString();
                  try {
                     int num = Integer.parseInt(text);
                     Log.i("",num+" is a number");
                  } catch (NumberFormatException e) {
                     Log.i("",text+" is not a number");
                  }
                  

                  【讨论】:

                    【解决方案11】:

                    我使用这个函数来检查小数点后两位的三位整数和浮点值。如果您不想限制位数,请从正则表达式中删除 {x,x}。

                    private boolean isNumeric(String string) {
                         if(Pattern.matches("\\d{1,3}((\\.\\d{1,2})?)", string))
                              return true;
                         else
                              return false;
                    }
                    

                    【讨论】:

                      【解决方案12】:

                      在activity里面简单的添加 android:inputType="数字"

                      【讨论】:

                        【解决方案13】:
                        /**
                         * If @param digit is null or not any of the numbers - @return false.
                         */
                        static boolean isNumber(String digit) {
                            try {
                                Double.parseDouble(digit);
                            } catch (NullPointerException | NumberFormatException ignored) {
                                return false;
                            }
                            return true;
                        }
                        

                        【讨论】:

                        • 纯代码答案是低质量的答案。
                        猜你喜欢
                        • 2011-07-13
                        • 1970-01-01
                        • 2023-03-27
                        • 2010-11-22
                        • 2011-02-09
                        • 1970-01-01
                        • 1970-01-01
                        • 2010-11-29
                        • 2021-09-14
                        相关资源
                        最近更新 更多