ตกลงนี่เป็นวิธีที่ดีกว่าในการจัดการกับรูปแบบสกุลเงินการกดแป้นพิมพ์แบบลบย้อนกลับ รหัสนี้อ้างอิงจากโค้ด @androidcurious ด้านบน ... แต่เกี่ยวข้องกับปัญหาบางอย่างที่เกี่ยวข้องกับการลบย้อนกลับและข้อยกเว้นการแยกวิเคราะห์บางอย่าง:
http://miguelt.blogspot.ca/2013/01/textwatcher-for-currency-masksformatting .html
[UPDATE] วิธีแก้ปัญหาก่อนหน้านี้มีปัญหา ... นี่คือ solutoin ที่ดีกว่า: 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());
}
}