Android自定义字符串格式

时间:2017-01-17 13:35:21

标签: android string android-edittext textview decimalformat

我在我的应用中有一个EditTextTextView我希望在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;
            }

1 个答案:

答案 0 :(得分:0)

这有效

        try {
                DecimalFormat df=new DecimalFormat("###,###,###,###");
                amountTextView.setText(df.format(Integer.parseInt(amountEditText.getText().toString())));
            } catch (NumberFormatException e){
                amountTextView.setText("");
                billAmount=0;
            }
相关问题