我从EditText
获取价值时遇到问题。当我按下回车后获得价值时,一切正常。当我尝试通过单击虚拟键盘外部来完成它时,所有事情都会起作用。我甚至在按下屏幕后看到在EditText窗口中编号为"0.00"
的数字,但突然出现错误。
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)
答案 0 :(得分:0)
我认为你得到的价值不是解析,而是
更改
cena2 = Float.valueOf(tvCena2.getText().toString());
到
cena2 = Float.parseFloat(tvCena2.getText().toString());
它应该有用。