找出是否填写了数字editText(findout if numeric editText is filled out or not)

我有一个名为countOfProduct的EditText (编号为editText类型,当用户想插入此时,只是keyborad显示的数量)和if中我想看到最终用户是否插入数字。 我用Google搜索并找到了许多解决方案,但它们都没有为我工作。

这是我的if:

if(!countOfProduct.getText().toString().equals("") || !countOfProduct.getText().toString().matches("")|| !countOfProduct.getText().toString().matches(null) || !countOfProduct.getText().toString().equals(null) || !countOfProduct.getText().toString().isEmpty() || !countOfProduct.getText().equals(null) || (countOfProduct.getText().length() ==0) || (countOfProduct.getText().toString().length() ==0))

我该怎么办?

I have a EditText named countOfProduct(Number editText type, when user want to insert into this, just numbers of keyborad display) and an if in which i want to see that end user insert a number or not. I googled and find many solutions, but none of them worked for me.

Here is my if:

if(!countOfProduct.getText().toString().equals("") || !countOfProduct.getText().toString().matches("")|| !countOfProduct.getText().toString().matches(null) || !countOfProduct.getText().toString().equals(null) || !countOfProduct.getText().toString().isEmpty() || !countOfProduct.getText().equals(null) || (countOfProduct.getText().length() ==0) || (countOfProduct.getText().toString().length() ==0))

what should i do?

最满意答案

试试:

String myString = countOfProduct.getText().toString(); if (!TextUtils.isEmpty(myString)) { if (tryParseInt(myString)) Log.d("IS A NUMBER", ""); } boolean tryParseInt(String value) { try { Integer.parseInt(value); return true; } catch (NumberFormatException e) { return false; } }

Try with:

String myString = countOfProduct.getText().toString(); if (!TextUtils.isEmpty(myString)) { if (tryParseInt(myString)) Log.d("IS A NUMBER", ""); } boolean tryParseInt(String value) { try { Integer.parseInt(value); return true; } catch (NumberFormatException e) { return false; } }

更多推荐