Java 带负数的EditText和TextWatcher

Java 带负数的EditText和TextWatcher,java,android,textwatcher,Java,Android,Textwatcher,我在将负整数输入到EditText时遇到问题,当我这样做时,我得到一个错误09-12 06:26:42.025:E/AndroidRuntime(18247):java.lang.NumberFormatException:无效整数:“-” xml文件: <EditText android:id="@+id/downAndDistanceStarting" android:layout_width="180dp" android:layout

我在将负整数输入到EditText时遇到问题,当我这样做时,我得到一个错误
09-12 06:26:42.025:E/AndroidRuntime(18247):java.lang.NumberFormatException:无效整数:“-”
xml文件:

<EditText
        android:id="@+id/downAndDistanceStarting"
        android:layout_width="180dp"
        android:layout_height="match_parent"
        android:ems="10"
        android:inputType="numberSigned"
        android:selectAllOnFocus="true"
        android:textSize="@dimen/enterText" >

我假设我的textWatcher有问题,它在每次键盘“按下”后都会“发送”我的输入,所以当我试图在完成之前输入-12时,它会发送“-”用于“解析”,这就是我的应用程序崩溃的原因,对吗?有人知道怎么修吗

是的,你是对的。将您的代码包装在try/catch块中,然后,当收到-时,它将抛出NumberFormatException并将被捕获

try
{
int w = Integer.parseInt(end.getText().toString());
}catch(NumberFormatException e)
{

}

我也面临同样的问题。在解析之前,我放置了一个if条件。这样,在输入第二个数字之前,它不会进行解析

end = (EditText) findViewById(R.id.downAndDistanceEnding);
public void afterTextChanged(Editable s) {
  if(!end.getText().toString().equals("-"));
  {
    int w = Integer.parseInt(end.getText().toString());
  } else { //do something  }
}
end = (EditText) findViewById(R.id.downAndDistanceEnding);
public void afterTextChanged(Editable s) {
  if(!end.getText().toString().equals("-"));
  {
    int w = Integer.parseInt(end.getText().toString());
  } else { //do something  }
}