立即验证检查 edittext 字段的输入(整数类型)

Immediate validation check on input to edittext field(integer type)

我想验证输入,如果输入的值在 9 到 50 之间,则输入文本有效(toast),否则 toast 无效。 我面临的问题是当我输入一些整数而不是 clear/erase 文本字段时,应用程序崩溃了。 主要 activity 代码在这里:

v1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

               /* int var1,var2;
                var1=Integer.parseInt(v1.getText().toString());
                var2=Integer.parseInt(v2.getText().toString());

                if (var1<9 && var2>50){
                    Toast.makeText(MainActivity.this,"invalid input ",Toast.LENGTH_SHORT).show();
                }*/
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
/*

                int var1,var2;
                var1=Integer.parseInt(v1.getText().toString());
                var2=Integer.parseInt(v2.getText().toString());
*/
//                String var1=v1.getText().toString();




               /* String inpt="";
                *//*for (int i = start;i<=before;i++){

                    inpt= s + inpt;

                }

*//*
//                for(String reslt:s.length())
                var1=Integer.parseInt(inpt);
*/


    int var1=0;

    var1 = Integer.parseInt(s.toString());


    if (var1 < 9 || var1 > 50) {
        Toast.makeText(MainActivity.this, "invalid input ", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(MainActivity.this, "Valid ", Toast.LENGTH_SHORT).show();

    }
}


            @Override
            public void afterTextChanged(Editable s) {



            }
        });

崩溃日志如下:

11-07 16:11:49.521 27518-27518/com.example.mts3_.p1_d3_app_bar E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.mts3_.p1_d3_app_bar, PID: 27518
                                                                             java.lang.NumberFormatException: Invalid int: ""
                                                                                 at java.lang.Integer.invalidInt(Integer.java:138)
                                                                                 at java.lang.Integer.parseInt(Integer.java:358)
                                                                                 at java.lang.Integer.parseInt(Integer.java:334)
                                                                                 at com.example.mts3_.p1_d3_app_bar.MainActivity.onTextChanged(MainActivity.java:82)
                                                                                 at android.widget.TextView.sendOnTextChanged(TextView.java:7894)
                                                                                 at android.widget.TextView.handleTextChanged(TextView.java:7954)
                                                                                 at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9776)
                                                                                 at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:964)
                                                                                 at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:515)
                                                                                 at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:216)
                                                                                 at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:33)
                                                                                 at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)
                                                                                 at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)
                                                                                 at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)
                                                                                 at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:138)
                                                                                 at android.widget.TextView.doKeyDown(TextView.java:5930)
                                                                                 at android.widget.TextView.onKeyDown(TextView.java:5743)
                                                                                 at android.view.KeyEvent.dispatch(KeyEvent.java:2651)
                                                                                 at android.view.View.dispatchKeyEvent(View.java:8412)
                                                                                 at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
                                                                                 at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
                                                                                 at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
                                                                                 at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
                                                                                 at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
                                                                                 at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
                                                                                 at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
                                                                                 at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2432)
                                                                                 at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1738)
                                                                                 at android.app.Activity.dispatchKeyEvent(Activity.java:2757)
                                                                                 at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
                                                                                 at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
                                                                                 at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
                                                                                 at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
                                                                                 at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2321)
                                                                                 at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4273)
                                                                                 at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4227)
                                                                                 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3767)
                                                                                 at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3830)
                                                                                 at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3786)
                                                                                 at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3913)
                                                                                 at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3794)
                                                                                 at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3970)
                                                                                 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3767)
                                                                                 at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3830)
                                                                                 at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3786)
                                                                                 at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3794)
                                                                                 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3767)
                                                                                 at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6165)
                                                                                 at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6139)
                                                                                 at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6110)
                                                                                 at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3533)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Loope

11-07 16:11:49.551 27518-27518/com.example.mts3_.p1_d3_app_bar V/Process: killProcess [27518] Callers=com.android.internal.os.RuntimeInit$ UncaughtHandler.uncaughtException:99 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 11-07 16:11:49.551 27518-27518/com.example.mts3_.p1_d3_app_bar I/Process:发送信号。 PID:27518 SIG:9

当您调用 Integer.parseInt() 时,代码会将您的字符串变量转换为整数,而 EditText 中的“”字符串无效。在调用 Integer.parseInt().

之前,您应该检查输入字符串是否不等于 ""

顺便说一句。更好的做法是编写您的自定义输入过滤器,它只允许使用 9 - 50

范围内的数字

你的问题是

java.lang.NumberFormatException: Invalid int: ""

所以当我们将String转换为int时,我们应该知道String是否为null。

当您在代码中使用 TextUtils.isEmpty(s.toString()) ? 0 : s.toString() 时,如果 s.toString() 为空,它将 return 0 。它不会导致 NumberFormatException 。并且如果 s.toString() 不为空,它将 return 正常。

判断s.toString是否为空

在您的代码中使用 var1 = Integer.parseInt(TextUtils.isEmpty(s.toString()) ? 0 : s.toString());

var1 = Integer.parseInt(TextUtils.isEmpty(s.toString()) ? 0 : s.toString());
if (var1 < 9 || var1 > 50) {
    Toast.makeText(MainActivity.this, "invalid input ", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(MainActivity.this, "Valid ", Toast.LENGTH_SHORT).show();
}

需要在 onTextChanged(override method) 中进行简单检查,以检查字段是否为空。 工作代码如下:

if(s.length()>0) {


            int var1 = 0;

            var1 = Integer.parseInt(s.toString());


            if (var1 < 9 || var1 > 50) {
                Toast.makeText(MainActivity.this, "invalid input ", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "Valid ", Toast.LENGTH_SHORT).show();

            }
        }else {Toast.makeText(MainActivity.this, "FIELD EMPTY ", Toast.LENGTH_SHORT).show();}