Android 固定小数点反向货币输入
Android reverse money input with fixed decimal
我想创建一个 Eddittext 来输入从左到右保留 2 位小数的货币值。如果没有值,它显示 0.00,并且当用户键入时,文本应根据以下规则更改:
我试过在类似问题中使用 TextWatcher 完成它,但我无法完成它,因为它在更新文本后一直调用 TextWatcher。
试试这个:
String yourStringToPutIntoTextView = String.format("%.2f", YourFloat);
举个例子:
List<Float> listTestValue = new ArrayList<Float>();
listTestValue.add(new Float(10));
listTestValue.add(new Float(10.10));
listTestValue.add(new Float(1010));
listTestValue.add(new Float(0));
listTestValue.add(new Float(0.9));
listTestValue.add(new Float(.12));
listTestValue.add(new Float(0.01));
for(Float f : listTestValue)
{
String s = String.format("%.2f", f);
System.out.println(s);
}
如果你没有 f = 0 的输入格式字符串,像这样:
String noInput = String.format("%.2f", (float)0);
注意数值必须是Float!
输出:
10,00
10,10
1010,00
0,00
0,90
0,12
0,01
我终于按照我想要的方式使用带有此代码的 TextWatcher,希望它对某人有所帮助:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if ((count - before) > 0) {
String text = s.toString().replace(',', '.');
text = text.replace("..", ".");
if (text.equals(".")) {
text = "0,00";
amount_field.setText(text);
amount_field.setSelection(2);
} else {
int counter = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '.') {
counter++;
if (counter > 1) {
break;
}
}
}
if (counter > 1) {
StringBuilder sb = new StringBuilder(text);
sb.deleteCharAt(start);
amount_field.setText(sb.toString().replace('.', ','));
amount_field.setSelection(start);
} else {
Float value = Float.valueOf(text);
String result = String.format("%.2f", value);
amount_field.setText(result.replace('.', ','));
if (start != result.length()) {
amount_field.setSelection(start + 1);
} else {
amount_field.setSelection(start);
}
}
}
}
}
我想创建一个 Eddittext 来输入从左到右保留 2 位小数的货币值。如果没有值,它显示 0.00,并且当用户键入时,文本应根据以下规则更改:
我试过在类似问题中使用 TextWatcher 完成它,但我无法完成它,因为它在更新文本后一直调用 TextWatcher。
试试这个:
String yourStringToPutIntoTextView = String.format("%.2f", YourFloat);
举个例子:
List<Float> listTestValue = new ArrayList<Float>();
listTestValue.add(new Float(10));
listTestValue.add(new Float(10.10));
listTestValue.add(new Float(1010));
listTestValue.add(new Float(0));
listTestValue.add(new Float(0.9));
listTestValue.add(new Float(.12));
listTestValue.add(new Float(0.01));
for(Float f : listTestValue)
{
String s = String.format("%.2f", f);
System.out.println(s);
}
如果你没有 f = 0 的输入格式字符串,像这样:
String noInput = String.format("%.2f", (float)0);
注意数值必须是Float!
输出:
10,00
10,10
1010,00
0,00
0,90
0,12
0,01
我终于按照我想要的方式使用带有此代码的 TextWatcher,希望它对某人有所帮助:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if ((count - before) > 0) {
String text = s.toString().replace(',', '.');
text = text.replace("..", ".");
if (text.equals(".")) {
text = "0,00";
amount_field.setText(text);
amount_field.setSelection(2);
} else {
int counter = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '.') {
counter++;
if (counter > 1) {
break;
}
}
}
if (counter > 1) {
StringBuilder sb = new StringBuilder(text);
sb.deleteCharAt(start);
amount_field.setText(sb.toString().replace('.', ','));
amount_field.setSelection(start);
} else {
Float value = Float.valueOf(text);
String result = String.format("%.2f", value);
amount_field.setText(result.replace('.', ','));
if (start != result.length()) {
amount_field.setSelection(start + 1);
} else {
amount_field.setSelection(start);
}
}
}
}
}