Java 在Android中使用EditText格式化货币字符串
我正在尝试在Android中格式化EditText的输入值,我想用货币值格式化输入,我已经尝试了以下方法:Java 在Android中使用EditText格式化货币字符串,java,android,string,formatting,android-edittext,Java,Android,String,Formatting,Android Edittext,我正在尝试在Android中格式化EditText的输入值,我想用货币值格式化输入,我已经尝试了以下方法: EditText minimo = (EditText) view.findViewById(R.id.minimo); Locale locale = new Locale("en", "UK"); NumberFormat fmt = NumberFormat.getCurrencyInstance(locale); Locale locale = new Locale("en"
EditText minimo = (EditText) view.findViewById(R.id.minimo);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
minimo.setText("", TextView.BufferType.valueOf(String.valueOf(fmt.format(TextView.BufferType.EDITABLE))));
日志:
EditText minimo = (EditText) view.findViewById(R.id.minimo);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
minimo.setText("", TextView.BufferType.valueOf(String.valueOf(fmt.format(TextView.BufferType.EDITABLE))));
试试这个:
EditText minimo = (EditText) view.findViewById(R.id.minimo);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
minimo.setText("", TextView.BufferType.valueOf(String.valueOf(fmt.format(TextView.BufferType.EDITABLE))));
将TextChangedListner
设置为:
EditText minimo = (EditText) view.findViewById(R.id.minimo);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
minimo.setText("", TextView.BufferType.valueOf(String.valueOf(fmt.format(TextView.BufferType.EDITABLE))));
minimo.addTextChangedListener(new NumberTextWatcher(minimo));
创建自定义TextWatcher
作为:
EditText minimo = (EditText) view.findViewById(R.id.minimo);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
minimo.setText("", TextView.BufferType.valueOf(String.valueOf(fmt.format(TextView.BufferType.EDITABLE))));
class NumberTextWatcher implements TextWatcher {
private DecimalFormat df;
private DecimalFormat dfnd;
private boolean hasFractionalPart;
private EditText et;
public NumberTextWatcher(EditText et)
{
df = new DecimalFormat("#,###.##");
df.setDecimalSeparatorAlwaysShown(true);
dfnd = new DecimalFormat("#,###");
this.et = et;
hasFractionalPart = false;
}
@SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";
public void afterTextChanged(Editable s)
{
et.removeTextChangedListener(this);
try {
int inilen, endlen;
inilen = et.getText().length();
String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
Number n = df.parse(v);
int cp = et.getSelectionStart();
if (hasFractionalPart) {
et.setText(df.format(n));
} else {
et.setText(dfnd.format(n));
}
endlen = et.getText().length();
int sel = (cp + (endlen - inilen));
if (sel > 0 && sel <= et.getText().length()) {
et.setSelection(sel);
} else {
// place cursor at the end?
et.setSelection(et.getText().length() - 1);
}
} catch (NumberFormatException nfe) {
// do nothing?
} catch (ParseException e) {
// do nothing?
}
et.addTextChangedListener(this);
}
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().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
{
hasFractionalPart = true;
} else {
hasFractionalPart = false;
}
}
}
class NumberTextWatcher实现TextWatcher{
专用分码格式df;
私有决策格式dfnd;
私有布尔部分;
私人编辑;
公用号码ExtWatcher(编辑文本et)
{
df=新的十进制格式(“#,###.##”);
df.setDecimalSeparatorAlwaysShown(真);
dfnd=新的十进制格式(“#,####”);
this.et=et;
hasstatilpart=false;
}
@抑制警告(“未使用”)
私有静态最终字符串标记=“NumberTextWatcher”;
公共无效后文本已更改(可编辑)
{
et.removeTextChangedListener(此);
试一试{
内伊尼伦,恩德伦;
inilen=et.getText().length();
String v=s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()),“”);
数字n=df.parse(v);
int cp=et.getSelectionStart();
if(hasstatilpart){
et.setText(df.format(n));
}否则{
et.setText(dfnd.format(n));
}
endlen=et.getText().length();
int sel=(cp+(endlen-inilen));
如果(sel>0&&sel谢谢你Rustam,这使错误消失了,但它没有做我想做的事情,即如果用户键入“1000000”,它必须将值格式化为“10.000,00”。我编辑了这个问题,我想要格式化的是可编辑值,这真的很好,它工作了,只有一件事,我可以替换格式“###,######,####,####?@Rustam但如果我删除TextView,它会抱怨并要求android.widget.TextView;