使用 TextWatcher 添加两个负数

发布于 2025-01-15 01:23:29 字数 1585 浏览 1 评论 0原文

我绝对是初学者。我将添加两个负数并使用 TextWatcher 显示结果。 当我尝试编辑第二个(负数)数字时,应用程序崩溃了。我不知道问题出在哪里。 主要活动java代码是 ...

package com.example.addnegativnumbers;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText Number1, Number2;
TextView Result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Number1 = findViewById(R.id.Number1);
    Number2 = findViewById(R.id.Number2);
    Result = findViewById(R.id.Result);

    TextWatcher textWatcher = 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) {

            if (!Number1.getText().toString().equals("") && !Number2.getText().toString().equals("")) {
                int temp1 = Integer.parseInt(Number1.getText().toString());
                int temp2 = Integer.parseInt(Number2.getText().toString());
                Result.setText(String.valueOf(temp1 + temp2));
            }
            ;
        }
        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
    Number1.addTextChangedListener(textWatcher);
    Number2.addTextChangedListener(textWatcher);
}

} ...

I'm absolute beginner. I will to add two negativ numbers and the result display with TextWatcher.
When I try to edit the second (negative) number, the app is crashes. I don't know where is the problem.
Main Activity java code is
...

package com.example.addnegativnumbers;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText Number1, Number2;
TextView Result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Number1 = findViewById(R.id.Number1);
    Number2 = findViewById(R.id.Number2);
    Result = findViewById(R.id.Result);

    TextWatcher textWatcher = 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) {

            if (!Number1.getText().toString().equals("") && !Number2.getText().toString().equals("")) {
                int temp1 = Integer.parseInt(Number1.getText().toString());
                int temp2 = Integer.parseInt(Number2.getText().toString());
                Result.setText(String.valueOf(temp1 + temp2));
            }
            ;
        }
        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
    Number1.addTextChangedListener(textWatcher);
    Number2.addTextChangedListener(textWatcher);
}

}
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无名指的心愿 2025-01-22 01:23:29

Activity_main.xml 的一部分:

<EditText
    android:id="@+id/Number1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="40dp"
    android:layout_marginTop="40dp"
    android:ems="10"
    android:hint="Enter number 1"
    android:inputType="numberSigned|textPersonName"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/Number2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:hint="Enter number 2"
    android:inputType="numberSigned|textPersonName"
    app:layout_constraintStart_toStartOf="@+id/Number1"
    app:layout_constraintTop_toBottomOf="@+id/Number1" />

Logcat 第 39 行的一部分

2022-03-17 06:55:26.959 21618-21618/com.example.addnegativnumbers     E/InputEventSender: Exception dispatching finished signal.
2022-03-17 06:55:26.960 21618-21618/com.example.addnegativnumbers E/MessageQueue-JNI:  Exception in MessageQueue callback: handleReceiveCallback
2022-03-17 06:55:26.968 21618-21618/com.example.addnegativnumbers E/MessageQueue-JNI: java.lang.NumberFormatException: For input string: "-"
    at java.lang.Integer.parseInt(Integer.java:513)
    at java.lang.Integer.parseInt(Integer.java:556)
    at com.example.addnegativnumbers.MainActivity$1.onTextChanged(MainActivity.java:39)

是:

     int temp2 = Integer.parseInt(Number2.getText().toString());

问题是字符串“-”

Part of activity_main.xml:

<EditText
    android:id="@+id/Number1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="40dp"
    android:layout_marginTop="40dp"
    android:ems="10"
    android:hint="Enter number 1"
    android:inputType="numberSigned|textPersonName"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/Number2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:hint="Enter number 2"
    android:inputType="numberSigned|textPersonName"
    app:layout_constraintStart_toStartOf="@+id/Number1"
    app:layout_constraintTop_toBottomOf="@+id/Number1" />

Part of Logcat

2022-03-17 06:55:26.959 21618-21618/com.example.addnegativnumbers     E/InputEventSender: Exception dispatching finished signal.
2022-03-17 06:55:26.960 21618-21618/com.example.addnegativnumbers E/MessageQueue-JNI:  Exception in MessageQueue callback: handleReceiveCallback
2022-03-17 06:55:26.968 21618-21618/com.example.addnegativnumbers E/MessageQueue-JNI: java.lang.NumberFormatException: For input string: "-"
    at java.lang.Integer.parseInt(Integer.java:513)
    at java.lang.Integer.parseInt(Integer.java:556)
    at com.example.addnegativnumbers.MainActivity$1.onTextChanged(MainActivity.java:39)

Line 39 is:

     int temp2 = Integer.parseInt(Number2.getText().toString());

The problem is the string "-"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文