16

我试过这个片段,但它不起作用

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 来检查值,我不希望它只接受数字!!

4

13 回答 13

22

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

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();
                }
            }
于 2016-10-05T07:13:27.970 回答
21

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

String regexStr = "^[0-9]*$";

if(et_number.getText().toString().trim().matches(regexStr))
{
    //write code here for success
}
else{
    // write code for failure
}
于 2013-07-25T09:35:19.680 回答
7

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

android:inputType="number"
于 2013-07-25T09:00:29.307 回答
5

试试这个正则表达式:

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!!!!!!!!!!!^^^");
 }
于 2013-07-25T09:33:43.470 回答
5

为此使用TextUtils类函数

TextUtils.isDigitsOnly("gifg");

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

于 2016-10-05T07:21:05.427 回答
2

使用这个方法

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

像这样 :

if (isNumber(input)) {
  // string is int
}
于 2018-02-09T15:36:38.097 回答
0

也许是这样的:

    String text = enteredID.getText().toString();

    if(text.matches("\\w+")){
      //--words--
    }else if (text.matches("\\d+")){
      //--numeric--
    }else {
      //-- something else --
    }

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

于 2013-07-25T09:01:41.457 回答
0

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

尝试使用: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);
于 2013-07-25T09:01:01.557 回答
0

在活动中只需添加 android:inputType="number"

于 2019-05-20T10:00:32.990 回答
0
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");
}
于 2017-08-23T05:45:47.173 回答
0

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

private boolean isNumeric(String string) {
     if(Pattern.matches("\\d{1,3}((\\.\\d{1,2})?)", string))
          return true;
     else
          return false;
}
于 2019-03-15T08:33:42.540 回答
0
/**
 * 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;
}
于 2020-09-23T12:51:27.383 回答
0
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

}
于 2016-06-22T11:20:39.523 回答