【问题标题】:Android Custom String FormatAndroid 自定义字符串格式
【发布时间】:2017-01-17 13:35:20
【问题描述】:

我的应用程序中有一个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;
            }

【问题讨论】:

    标签: android string android-edittext textview decimalformat


    【解决方案1】:

    这行得通

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

    【讨论】:

      猜你喜欢
      • 2012-08-28
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多