Check if an Edit Text that only accepts number is

2019-02-20 15:57发布

问题:

I'm building an application for receiving grades and I want to make sure that the Edit Texts are not empty and the values are less or equal to 100 I wrote this line but it crashes the application

if(Integer.parseInt(editText.gettext().toString()) > 100 || editText.getText().toString().trim().length() == 0)
{
//Error message for example
} 

and this is the logCat

09-04 18:21:06.331 8649-8649/com.example.nima.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.nima.myapplication, PID: 8649 java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:3827) at android.view.View.performClick(View.java:4442) at android.view.View$PerformClick.run(View.java:18473) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at android.view.View$1.onClick(View.java:3822)             at android.view.View.performClick(View.java:4442)             at android.view.View$PerformClick.run(View.java:18473)             at android.os.Handler.handleCallback(Handler.java:733)             at android.os.Handler.dispatchMessage(Handler.java:95)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5103)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)             at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NumberFormatException: Invalid int: "" at java.lang.Integer.invalidInt(Integer.java:137) at java.lang.Integer.parseInt(Integer.java:358) at java.lang.Integer.parseInt(Integer.java:331) at com.example.nima.myapplication.MainActivity.me(MainActivity.java:22)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at android.view.View$1.onClick(View.java:3822)             at android.view.View.performClick(View.java:4442)             at android.view.View$PerformClick.run(View.java:18473)             at android.os.Handler.handleCallback(Handler.java:733)             at android.os.Handler.dispatchMessage(Handler.java:95)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5103)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)             at dalvik.system.NativeStart.main(Native Method)

回答1:

In your xml where you have declared edittext make sure that you put the following attribute in edittext element

android:inputType="number"

And change above code to this :

 if(editText.getText().toString().trim().isEmpty() || Integer.parseInt(editText.gettext().toString()) > 100 )
    {
    //Error message for example
    } 

you first need to check if text is not empty



回答2:

First You Put In Your XML EditText Input Type=Number

Then Write This Code

If(edittext.getString().toString().Trim().lenght()>0)
{
     If(Integer.parseInt(editText.gettext().toString()) <= 100 )
     {

     }
}


回答3:

You only have to switch the 2nd ans 1st expressions of the if. Because when the program runs it checks firstly the 1st expression and then the 2nd.

So... when you don't have any value on the editText the getText().toString() return this "" and its impossible to parse to Int that value so you have to check first the length() and after that check if the number is bigger than 100

if(editText.getText().toString().trim().isEmpty() || Integer.parseInt(editText.gettext().toString()) > 100 )
{
//Error message for example
} 


回答4:

If you want to control what digit should enter use android:digits=""

android:inputType="number" enforce to open number type keyboard still user can press other char like "#,." than may cause NumberFormatException

<EditText
    android:inputType="number"
    android:digits="0123456789"
/>

android:digits="0123456789" only accepts 0123456789 in EditText, not any other char

Before converting any string integer you must check if it is empty

String strNumber=editText.getText().toString().trim();
 if(TextUtils.isEmpty(strNumber) || Integer.parseInt(strNumber)>100){
   // show your error message
}


回答5:

Your logcat states that you are entering a String where the code requires a Integer.

Caused by: java.lang.NumberFormatException:

What i would suggest is store the EditText value in a different String and then check typecast it into Integer, many a times the compiler gets confused there.

Hope this helps :)