當按下點號或逗號按鈕時,按下了inputType =「number」或inputType =「number | Decimal」。它未能與android:digits =「。」,一起使用。無法使用EditText輸入小數點inputType =「number」Android
EditText包含一個格式化數字的textwatcher。文字如下:
mEditWithdrawalAmount.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().equals(current)) {
mEditWithdrawalAmount.removeTextChangedListener(this);
String replaceable = String.format("[%s .\\s]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
String cleanString = s.toString().replaceAll(replaceable, "").replace("R","").replace(",","");
double parsed;
try {
parsed = Double.parseDouble(cleanString);
} catch (NumberFormatException e) {
parsed = 0.00;
}
String formatted = Utils.formatCurrency(parsed);
current = formatted;
mEditWithdrawalAmount.setText(formatted);
mEditWithdrawalAmount.setSelection(formatted.length());
// Do whatever you want with position
mEditWithdrawalAmount.addTextChangedListener(this);
}
}
});
問題是edittext必須允許帶小數點的數字。
- 實際結果是:R1000 000
- 期望的結果是R1000 000.00或R1000 000.40
在XML editext安卓的inputType = 「numberDecimal」 –
安卓的inputType = 「numberDecimal」 只有工作沒有onTextChange監聽 – BigWiz