わかりました。これは、通貨形式、削除-後方キーストロークを処理するためのより良い方法です。コードは上記の@androidcurious 'コードに基づいています...しかし、後方削除といくつかの解析例外に関連するいくつかの問題を扱います:http:
//miguelt.blogspot.ca/2013/01/textwatcher-for-currency-masksformatting .html
[UPDATE]以前のソリューションにはいくつかの問題がありました...これはより良いソリューションです:http://miguelt.blogspot.ca/2013/02/update-textwatcher-for-currency.html
そして...ここに詳細:
このアプローチは、従来のAndroidメカニズムを使用しているため、優れています。アイデアは、ユーザーがビューを存在させた後に値をフォーマットすることです。
数値を制限するために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クラスを使用していることに注意してください。実装は「Amounts、Taxes」にあります。
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);
FILTERSとON_FOCUSは、任意の数のEditTextビューで再利用できます。
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());
}
}