好的,这是处理货币格式(删除后退击键)的更好方法。该代码基于上面的@androidcurious'代码...但是,处理与向后删除和某些解析异常有关的一些问题:http :
//miguelt.blogspot.ca/2013/01/textwatcher-for-currency-masksformatting .html
[更新]先前的解决方案存在一些问题...这是一个更好的解决方案:http ://miguelt.blogspot.ca/2013/02/update-textwatcher-for-currency.html
而且...这是细节:
这种方法更好,因为它使用了常规的Android机制。这个想法是在用户存在View之后格式化值。
定义一个InputFilter来限制数值-在大多数情况下这是必需的,因为屏幕不够大,无法容纳较长的EditText视图。这可以是静态内部类,也可以是另一个普通类:
class NumericRangeFilter implements InputFilter {
private final double maximum;
private final double minimum;
NumericRangeFilter() {
this(0.00, 999999.99);
}
NumericRangeFilter(double p_min, double p_max) {
maximum = p_max;
minimum = p_min;
}
@Override
public CharSequence filter(
CharSequence p_source, int p_start,
int p_end, Spanned p_dest, int p_dstart, int p_dend
) {
try {
String v_valueStr = p_dest.toString().concat(p_source.toString());
double v_value = Double.parseDouble(v_valueStr);
if (v_value<=maximum && v_value>=minimum) {
return null;
}
} catch (NumberFormatException p_ex) {
}
return "";
}
}
定义一个将实现View.OnFocusChangeListener的类(内部静态类或仅一个类)。请注意,我使用的是Utils类-可以在“金额,税项”中找到该实现。
class AmountOnFocusChangeListener implements View.OnFocusChangeListener {
@Override
public void onFocusChange(View p_view, boolean p_hasFocus) {
EditText v_amountView = (EditText)p_view;
if (p_hasFocus) {
String v_value = v_amountView.getText().toString();
int v_cents = Utils.parseAmountToCents(v_value);
v_value = Utils.formatCentsToAmount(v_cents);
v_amountView.setText(v_value);
v_amountView.selectAll();
} else {
String v_value = v_amountView.getText().toString();
int v_cents = Utils.parseAmountToCents(v_value);
v_value = Utils.formatCentsToCurrency(v_cents);
v_amountView.setText(v_value);
}
}
}
此类将在编辑时删除货币格式-依靠标准机制。当用户退出时,将重新应用货币格式。
最好定义一些静态变量以最大程度地减少实例数量:
static final InputFilter[] FILTERS = new InputFilter[] {new NumericRangeFilter()};
static final View.OnFocusChangeListener ON_FOCUS = new AmountOnFocusChangeListener();
最后,在onCreateView(...)中:
EditText mAmountView = ....
mAmountView.setFilters(FILTERS);
mAmountView.setOnFocusChangeListener(ON_FOCUS);
您可以在任意数量的EditText视图上重用FILTERS和ON_FOCUS。
这是Utils类:
public class Utils {
private static final NumberFormat FORMAT_CURRENCY = NumberFormat.getCurrencyInstance();
public static int parseAmountToCents(String p_value) {
try {
Number v_value = FORMAT_CURRENCY.parse(p_value);
BigDecimal v_bigDec = new BigDecimal(v_value.doubleValue());
v_bigDec = v_bigDec.setScale(2, BigDecimal.ROUND_HALF_UP);
return v_bigDec.movePointRight(2).intValue();
} catch (ParseException p_ex) {
try {
BigDecimal v_bigDec = new BigDecimal(p_value);
v_bigDec = v_bigDec.setScale(2, BigDecimal.ROUND_HALF_UP);
return v_bigDec.movePointRight(2).intValue();
} catch (NumberFormatException p_ex1) {
return -1;
}
}
}
public static String formatCentsToAmount(int p_value) {
BigDecimal v_bigDec = new BigDecimal(p_value);
v_bigDec = v_bigDec.setScale(2, BigDecimal.ROUND_HALF_UP);
v_bigDec = v_bigDec.movePointLeft(2);
String v_currency = FORMAT_CURRENCY.format(v_bigDec.doubleValue());
return v_currency.replace(FORMAT_CURRENCY.getCurrency().getSymbol(), "").replace(",", "");
}
public static String formatCentsToCurrency(int p_value) {
BigDecimal v_bigDec = new BigDecimal(p_value);
v_bigDec = v_bigDec.setScale(2, BigDecimal.ROUND_HALF_UP);
v_bigDec = v_bigDec.movePointLeft(2);
return FORMAT_CURRENCY.format(v_bigDec.doubleValue());
}
}