좋아, 통화 형식을 처리하는 더 좋은 방법, 뒤로 삭제 키 입력이 있습니다. 이 코드는 위의 @androidcurious '코드를 기반으로합니다 ... 그러나 역방향 삭제 및 일부 구문 분석 예외와 관련된 몇 가지 문제를 다룹니다.
http://miguelt.blogspot.ca/2013/01/textwatcher-for-currency-masksformatting .html
[업데이트] 이전 솔루션에는 몇 가지 문제가있었습니다 ... 이것은 더 나은 솔루션입니다 : 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);
여러 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());
}
}