[英]Android EditText money input Regex without “$” symbol
I want to enter decimal numbers like "23.50", "250.75". 我想输入十进制数字,例如“ 23.50”,“ 250.75”。 The following code provide this but it put front of numbers a "$" symbol like "$23.50", "$250.75" .. I want to remove dollar symbol and tried a few way but i couldn't.
下面的代码提供了这一点,但是它在数字的前面放置了一个“ $”符号,例如“ $ 23.50”,“ $ 250.75” ..我想删除美元符号并尝试了几种方法,但是我做不到。 How can i change regex and displaying?
如何更改正则表达式和显示?
the following codde : 以下代码:
inputAmount.setRawInputType(Configuration.KEYBOARD_12KEY);
inputAmount.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?"))
{
String userInput= ""+s.toString().replaceAll("[^\\d]", "");
StringBuilder cashAmountBuilder = new StringBuilder(userInput);
while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
cashAmountBuilder.deleteCharAt(0);
}
while (cashAmountBuilder.length() < 3) {
cashAmountBuilder.insert(0, '0');
}
cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
cashAmountBuilder.insert(0,'$');
inputAmount.setText(cashAmountBuilder.toString());
// keeps the cursor always to the right
Selection.setSelection(inputAmount.getText(), cashAmountBuilder.toString().length());
}
}
});
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().matches("^\\ (\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
{
String userInput= ""+s.toString().replaceAll("[^\\d]", "");
StringBuilder cashAmountBuilder = new StringBuilder(userInput);
while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
cashAmountBuilder.deleteCharAt(0);
}
while (cashAmountBuilder.length() < 3) {
cashAmountBuilder.insert(0, '0');
}
cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
cashAmountBuilder.insert(0, ' ');
edAmountAddMoney.setText(cashAmountBuilder.toString());
// keeps the cursor always to the right
Selection.setSelection(edAmountAddMoney.getText(), cashAmountBuilder.toString().length());
}
}
i use space instead of $ and it work for me 我用空格代替$,它对我有用
Remove the line: 删除行:
cashAmountBuilder.insert(0,'$');
And remove the \\\\$
from your regular expression. 并从正则表达式中删除
\\\\$
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.