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;
}
你能否請你發佈你的代碼,你在做什麼? – 2012-03-04 06:47:03
捕獲關鍵事件是否有效? http://stackoverflow.com/questions/5419766/how-to-capture-soft-keyboard-input-in-a-view – AndreiBogdan 2012-03-04 07:11:31
請參閱http://stackoverflow.com/questions/3821539/decimal-separator-comma-with -numberdecimal-的inputType合的EditText – 2012-03-04 17:00:04