2012-03-04 56 views
0

这是一个问题的两个部分:如何控制Android中EditText的输入?

1)我目前使用

android:inputType="numberDecimal|numberSigned" 

控制在我的应用程序的输入。

但是,我一直收到NumberFormatExceptions用户的崩溃报告。 其中之一是:

Caused by: java.lang.NumberFormatException: Invalid float: "2,7" 

所以很明显用户以某种方式绕过我的措施来控制输入和使用“”而不是“”我已经暂时使用

String.replace(",", ".") 

试图解决它,但我仍然得到NumberFormatExceptions。

我该怎么办?

2)如何强制软键盘应用程序只显示数字键盘,“ - ”和“。”?

XML:

<EditText 
     android:id="@+id/speed" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:inputType="number|numberDecimal" > 

在活动时间:

public void onClickCalc(View view) 
{ 
    switch(view.getId()) 
    { 
       String sp = speed.getText().toString().replace(",", "."); 
       if(functions.isNotValid(sp)) 
       { 
        Toast.makeText(this, "Invalid input.", Toast.LENGTH_LONG).show(); 
        return; 
       } 

       float speed = Float.parseFloat(sp); 

... logic here 
} 

在类功能:

public static boolean isNotValid(String editable) 
{ 
    try 
    { 
     float x = Float.parseFloat(editable); 
    } 
    catch(NumberFormatException nFE) 
    { 
     return true; 
    } 

    return false; 
} 
+0

你能否请你发布你的代码,你在做什么? – 2012-03-04 06:47:03

+0

捕获关键事件是否有效? http://stackoverflow.com/questions/5419766/how-to-capture-soft-keyboard-input-in-a-view – AndreiBogdan 2012-03-04 07:11:31

+1

请参阅http://stackoverflow.com/questions/3821539/decimal-separator-comma-with -numberdecimal-的inputType合的EditText – 2012-03-04 17:00:04

回答

0

只是回答关闭此。肯顿王子的回复很好。