Android 自定义字符串格式
Android Custom String Format
我的应用程序中有一个 EditText
和 TextView
,我想在 EditText
中输入数字,这个数字显示自定义格式,但不是。
private int billAmount = 0;
private TextView amountTextView;
amountTextView = (TextView) findViewById(R.id.amountTextView);
amountEditText = (EditText) findViewById(R.id.amountEditText);
amountEditText.addTextChangedListener(amountEditTextWatcher);
private final TextWatcher amountEditTextWatcher=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence,
int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence,
int i, int i1, int i2) {
try {
DecimalFormat df=new DecimalFormat("###,###,###,###");
df.format(billAmount);
amountTextView.setText(amountEditText.getText().toString());
billAmount=Integer.parseInt(amountEditText.getText().toString());
}catch (NumberFormatException e){
amountTextView.setText("");
billAmount=0;
}
这个有效
try {
DecimalFormat df=new DecimalFormat("###,###,###,###");
amountTextView.setText(df.format(Integer.parseInt(amountEditText.getText().toString())));
} catch (NumberFormatException e){
amountTextView.setText("");
billAmount=0;
}
我的应用程序中有一个 EditText
和 TextView
,我想在 EditText
中输入数字,这个数字显示自定义格式,但不是。
private int billAmount = 0;
private TextView amountTextView;
amountTextView = (TextView) findViewById(R.id.amountTextView);
amountEditText = (EditText) findViewById(R.id.amountEditText);
amountEditText.addTextChangedListener(amountEditTextWatcher);
private final TextWatcher amountEditTextWatcher=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence,
int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence,
int i, int i1, int i2) {
try {
DecimalFormat df=new DecimalFormat("###,###,###,###");
df.format(billAmount);
amountTextView.setText(amountEditText.getText().toString());
billAmount=Integer.parseInt(amountEditText.getText().toString());
}catch (NumberFormatException e){
amountTextView.setText("");
billAmount=0;
}
这个有效
try {
DecimalFormat df=new DecimalFormat("###,###,###,###");
amountTextView.setText(df.format(Integer.parseInt(amountEditText.getText().toString())));
} catch (NumberFormatException e){
amountTextView.setText("");
billAmount=0;
}