edittext中的负值

时间:2013-02-04 13:13:51

标签: android android-edittext

转换是其他类,我在MainActivity;

工作

当我清除edittext然后输入“ - ”然后它会产生错误,但是当我输入一些值然后在edittext的第一个位置然后它不会产生错误,plz帮助我附加我的错误日志。

Conversion conversion;


conversion = new Conversion();


@Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
if(edittext.length()!=0){
String Result =conversion.temp(spinner1.getSelectedItemPosition(),
        spinner2.getSelectedItemPosition(),edittext.getText().toString());
 textView.setText(Result);
 }

 }

这是我的功能

public String temp(int ddfrom,int ddto,String edittextvalue) {

float s;
    float inputValue = Float.parseFloat(edittextvalue);
    s = rate*inputValue;
return String.valueOf(s);

}
    02-04 12:56:33.029: E/AndroidRuntime(317): Uncaught handler: thread main exiting due to uncaught exception
      02-04 12:56:33.119: E/AndroidRuntime(317): java.lang.NumberFormatException: -
      02-04 12:56:33.119: E/AndroidRuntime(317):    at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:149)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:319)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at java.lang.Float.parseFloat(Float.java:288)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.sample.unitconverter.Conversion.timee(Conversion.java:651)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.sample.unitconverter.UnitConverter$6.onTextChanged(UnitConverter.java:397)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.widget.TextView.handleTextChanged(TextView.java:6172)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:578)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:231)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at      com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:57)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.os.Handler.dispatchMessage(Handler.java:99)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.os.Looper.loop(Looper.java:123)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at android.app.ActivityThread.main(ActivityThread.java:4363)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at java.lang.reflect.Method.invokeNative(Native Method)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at java.lang.reflect.Method.invoke(Method.java:521)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
      02-04 12:56:33.119: E/AndroidRuntime(317):    at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:1)

这是因为“ - ”无法解析浮动而你得到NumberFormatException。

public String temp(int ddfrom,int ddto,String edittextvalue) {
    float s;
    try {
        float inputValue = Float.parseFloat(edittextvalue);
        s = rate*inputValue;
        return String.valueOf(s);
    }
    catch (NumberFormatException nfe) {
           //Error handling.
    }
    return null;
}

答案 1 :(得分:1)

一开始你的方法应该是

public String temp(int ddfrom,int ddto,String edittextvalue) {
float s;
try {
    float inputValue = Float.parseFloat(edittextvalue);
    s = rate*inputValue;
    return String.valueOf(s);
catch (NumberFormatException nfe) {
       //Error handling.
}
return "";

}

将侦听器代码更新为

@Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
if(edittext.length()!=0){
     if(edittext.length()==1 && "-".equals(edittext.getText().toString())) {
         //Nothing to do
     } else {

         String Result =conversion.temp(spinner1.getSelectedItemPosition(),
        spinner2.getSelectedItemPosition(),edittext.getText().toString());
        textView.setText(Result);
     }
 }

答案 2 :(得分:0)

我不确定你在做什么,但每次在onTextChanged函数中更改文本时都要解析一个浮点数(调用函数temp

修复函数temp以返回0,例如,当存在解析错误时。发生这种情况是因为字符串" - "不会被解析为Float。

public String temp(int ddfrom,int ddto,String edittextvalue) {
        try {
        float s;
        float inputValue = Float.parseFloat(edittextvalue);
        s = rate*inputValue;
        return s;
    } catch(Exception ex) {
        return "0";
    }

}

答案 3 :(得分:0)

如果您使用android:inputType="numberDecimal",则不允许使用负数。在这种情况下,您应该添加:android:inputType="numberDecimal|numberSigned" 这将适用于您的情况