EditText AfterTextChanged问题

时间:2017-07-03 22:27:46

标签: android android-edittext

我正在尝试使用两个EditText字段创建一个简单的时间计算器应用程序。下面的代码只是一个普通的计算器,它累加了两个数字,我掀起来测试文本更改的监听器。出于某种原因,每次我运行此应用程序并在EditText字段中输入一个数字时,它只会崩溃并询问我是否要重新启动应用程序。我只是想让它在每次用户输入数字时更新总数,而不必按下按钮。有人可以帮我找到错误吗?我已经初始化了onCreate方法之外的前几个变量。

记录错误:

  07-03 21:06:45.499 2397-2397/com.example.thesoulreaper.swimmingcalculator E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.thesoulreaper.swimmingcalculator, PID: 2397
    java.lang.NumberFormatException: empty String
        at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
        at java.lang.Float.parseFloat(Float.java:459)
        at com.example.thesoulreaper.swimmingcalculator.OneHundredCalculatorActivity$3.afterTextChanged(OneHundredCalculatorActivity.java:103)
        at android.widget.TextView.sendAfterTextChanged(TextView.java:8202)
        at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10381)
        at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1218)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:579)
        at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:230)
        at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:229)
        at android.view.inputmethod.BaseInputConnection.deleteSurroundingText(BaseInputConnection.java:251)
        at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:451)
        at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:91)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

我的代码:

    fiftyEditText = (EditText) findViewById(R.id.FiftyEditText);
    hundredEditText = (EditText) findViewById(R.id.HundredEditText);
    totalTextView = (TextView) findViewById(R.id.totalTextView);

    fiftyEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if(Integer.parseInt(hundredEditText.getText().toString()) > 0 && Integer.parseInt(fiftyEditText.getText().toString()) > 0) {
                int total = Integer.parseInt(hundredEditText.getText().toString()) + Integer.parseInt(fiftyEditText.getText().toString());
                totalTextView.setText(total);
            } else if(Integer.parseInt(hundredEditText.getText().toString()) <= 0 && Integer.parseInt(fiftyEditText.getText().toString()) > 0) {
                totalTextView.setText(Integer.parseInt(fiftyEditText.getText().toString()));
            } else if(Integer.parseInt(hundredEditText.getText().toString()) > 0 && Integer.parseInt(fiftyEditText.getText().toString()) <= 0) {
                totalTextView.setText(Integer.parseInt(hundredEditText.getText().toString()));
            } else if (Integer.parseInt(hundredEditText.getText().toString()) <= 0 && Integer.parseInt(fiftyEditText.getText().toString()) <= 0) {
                totalTextView.setText(0);
            }
        }
    });

    hundredEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if(Integer.parseInt(hundredEditText.getText().toString()) > 0 && Integer.parseInt(fiftyEditText.getText().toString()) > 0) {
                int total = Integer.parseInt(hundredEditText.getText().toString()) + Integer.parseInt(fiftyEditText.getText().toString());
                totalTextView.setText(total);
            } else if(Integer.parseInt(hundredEditText.getText().toString()) <= 0 && Integer.parseInt(fiftyEditText.getText().toString()) > 0) {
                totalTextView.setText(Integer.parseInt(fiftyEditText.getText().toString()));
            } else if(Integer.parseInt(hundredEditText.getText().toString()) > 0 && Integer.parseInt(fiftyEditText.getText().toString()) <= 0) {
                totalTextView.setText(Integer.parseInt(hundredEditText.getText().toString()));
            } else if (Integer.parseInt(hundredEditText.getText().toString()) <= 0 && Integer.parseInt(fiftyEditText.getText().toString()) <= 0) {
                totalTextView.setText(0);
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

你的应用程序崩溃是因为你试图将空字符串转换为整数,所以如果字符串不为空则添加一个检查

@Override
public void afterTextChanged(Editable editable) {

    int hundredEditTextValue =  hundredEditText.getText().toString().isEmpty() ? 0 : Integer.parseInt(hundredEditText.getText().toString());
    int fiftyEditTextValue =  fiftyEditText.getText().toString().isEmpty() ? 0 : Integer.parseInt(fiftyEditText.getText().toString());

    if(hundredEditTextValue > 0 && fiftyEditTextValue > 0) {
        int total = hundredEditTextValue + fiftyEditTextValue;
        totalTextView.setText(String.valueOf(total));
    } else if(hundredEditTextValue <= 0 && fiftyEditTextValue > 0) {
        totalTextView.setText(String.valueOf(fiftyEditTextValue));
    } else if(hundredEditTextValue > 0 && fiftyEditTextValue <= 0) {
        totalTextView.setText(String.valueOf(hundredEditTextValue));
    } else if (hundredEditTextValue <= 0 && fiftyEditTextValue <= 0) {
        totalTextView.setText(String.valueOf(0));
    }
}

只是为了让你的edittext只接受数字在你的xml

中添加

<强>机器人:的inputType =&#34;数&#34;

<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   .
   .
   android:inputType="number" />