Hello Friends我使用以下代码显示以货币形式输入的金额。
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
{
String userInput= ""+s.toString().replaceAll("[^\\d]", "");
StringBuilder cashAmountBuilder = new StringBuilder(userInput);
while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
cashAmountBuilder.deleteCharAt(0);
}
while (cashAmountBuilder.length() < 3) {
cashAmountBuilder.insert(0, '0');
}
cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
cashAmountBuilder.insert(0, '$');
editAmount.setText(cashAmountBuilder.toString());
editAmount.setTextKeepState(cashAmountBuilder.toString());
Selection.setSelection(editAmount.getText(), cashAmountBuilder.toString().length());
}
}
符号带有“$”前缀的问题我要么想用新的INR符号替换或空白。我尝试用空白替换cashAmountBuilder.insert(0, '$');
代码补丁,它给我编译错误。请帮我一样。
谢谢,
答案 0 :(得分:0)
Guys找到了解决这个问题的解决方案。想和大家分享一下。
editAmount=(EditText)findViewById(R.id.editTextPaymentAmount);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
editAmount.setRawInputType(Configuration.KEYBOARD_12KEY);
editAmount.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)){
editAmount.removeTextChangedListener((TextWatcher) this);
// String replaceable = String.format("[%s,.]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
// String cleanString = s.toString().replaceAll(replaceable, "");
String cleanString = s.toString().replaceAll("[$,.]", "");
// String cleanString = s.toString(). replaceAll("[^\\d]", "");
BigDecimal parsed = new BigDecimal(cleanString).setScale(2,BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100),BigDecimal.ROUND_FLOOR);
String formato = NumberFormat.getCurrencyInstance().format(parsed);
System.out.println(formato);
current = formato;
String newSt=formato.substring(1);
editAmount.setText(newSt);
editAmount.setSelection(newSt.length());
editAmount.addTextChangedListener((TextWatcher) this);
}
}
});
}
public void priceClick(View view) {
editAmount.addTextChangedListener(new TextWatcher(){
DecimalFormat dec = new DecimalFormat("0.00");
private String current = "";
public void afterTextChanged(Editable arg0) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)){
editAmount.removeTextChangedListener((TextWatcher) this);
String cleanString = s.toString().replaceAll("[$,.]", "");
BigDecimal parsed = new BigDecimal(cleanString).setScale(2,BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100),BigDecimal.ROUND_FLOOR);
String formato = NumberFormat.getCurrencyInstance().format(parsed);
current = formato;
editAmount.setText(formato);
editAmount.setSelection(formato.length());
editAmount.addTextChangedListener((TextWatcher) this);
}
}
});
}