0

我从EditText. 当我按回车后获得价值时,一切正常。当我尝试通过单击虚拟键盘外部进行操作时,一切都搞砸了。"0.00"我什至在按下屏幕后在 EditText 窗口中看到格式化为的数字,但突然出现错误。我认为这可能有问题

cena2 = Float.valueOf(tvCena2.getText().toString());

因为当我设置cena2=12它工作正常。

public boolean dispatchTouchEvent(MotionEvent event) {
    View v = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);

    if (v instanceof EditText) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];

        Log.d("Activity", "Touch event "+event.getRawX()+","+event.getRawY()+" "+x+","+y+" rect "+w.getLeft()+","+w.getTop()+","+w.getRight()+","+w.getBottom()+" coords "+scrcoords[0]+","+scrcoords[1]);
        if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) { 

            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
        }
        if (checkEmpty(tvCena2)) {
            DecimalFormat df = new DecimalFormat("0.00");
            cena2 = Float.valueOf(tvCena2.getText().toString());
        //  cena2=12;
            tvCena2.setText(String.valueOf(df.format(cena2)));
        } else {
            cena2=0;
        }
    }

日志:

01-21 16:16:25.797: E/AndroidRuntime(13140): FATAL EXCEPTION: main
01-21 16:16:25.797: E/AndroidRuntime(13140): java.lang.NumberFormatException
01-21 16:16:25.797: E/AndroidRuntime(13140):    at org.apache.harmony.luni.util.FloatingPointParser.parseFltImpl(Native Method)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:321)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at java.lang.Float.parseFloat(Float.java:323)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at java.lang.Float.valueOf(Float.java:362)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at com.imprezowa.pizza.lukis.GlowneOkno.dispatchTouchEvent(GlowneOkno.java:264)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1695)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2217)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1901)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at android.os.Looper.loop(Looper.java:130)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at android.app.ActivityThread.main(ActivityThread.java:3701)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at java.lang.reflect.Method.invokeNative(Native Method)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at java.lang.reflect.Method.invoke(Method.java:507)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
01-21 16:16:25.797: E/AndroidRuntime(13140):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

我认为你得到 valueOf 而不是解析,

改变

cena2 = Float.valueOf(tvCena2.getText().toString());

cena2 = Float.parseFloat(tvCena2.getText().toString());

它应该工作。

于 2013-01-22T09:09:48.737 回答