我正在学习Android。我有一个edittext来输入金钱,如果我输入输入数字为1000,结果必须是1.000,如果我输入输入为1000000,结果必须是1.000.000。
当我输入时,从最后到开头的每3个字符必须有一个“。”。
答案 0 :(得分:1)
您可以使用InputFilter
public class CurrencyFormat implements InputFilter {
Pattern mPattern = Pattern.compile("(0|[1-9]+[0-9]*)?(\\.[0-9]{0,2})?");
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
String result =
dest.subSequence(0, dstart)
+ source.toString()
+ dest.subSequence(dend, dest.length());
Matcher matcher = mPattern.matcher(result);
if (!matcher.matches()) return dest.subSequence(dstart, dend);
return null;
}
}
您可以设置以下过滤器:
editText.setFilters(new InputFilter[] {new CurrencyFormat()});
如果您觉得这很有用,请接受它。
答案 1 :(得分:0)
public class CurrencyFormatInputFilter implements InputFilter {
Pattern mPattern = Pattern.compile("(0|[1-9]+[0-9]*)?(\\.[0-9]{0,2})?");
@Override
public CharSequence filter(
CharSequence source,
int start,
int end,
Spanned dest,
int dstart,
int dend) {
String result =
dest.subSequence(0, dstart)
+ source.toString()
+ dest.subSequence(dend, dest.length());
Matcher matcher = mPattern.matcher(result);
if (!matcher.matches()) return dest.subSequence(dstart, dend);
return null;
}
}
答案 2 :(得分:0)
String.format("%,d",currencyInteger);