2

当我更改 edittext 的值时,我想添加逗号分隔值和小数点?

当任何人根据edittext逗号中的数字更改editext中的值时,我想实时执行它应该添加或删除吗?

有没有办法将逗号作为小数分隔符?

4

1 回答 1

4

请在您的班级中实现以下所有功能。

final EditText editText1 = (EditText) findViewById(R.id.editText1);
        editText1.setText("1,200.40");

        editText1.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2,
                    int arg3) {
                insertCommaIntoNumber(editText1,s);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }
        });

private void insertCommaIntoNumber(EditText etText,CharSequence s)
    {
        try {
            if (s.toString().length() > 0) 
            {
                String convertedStr = s.toString();
                if (s.toString().contains(".")) 
                {
                    if(chkConvert(s.toString()))
                        convertedStr = customFormat("###,###.##",Double.parseDouble(s.toString().replace(",","")));
                } 
                else
                {
                    convertedStr = customFormat("###,###.##", Double.parseDouble(s.toString().replace(",","")));
                }

                if (!etText.getText().toString().equals(convertedStr) && convertedStr.length() > 0) {
                    etText.setText(convertedStr);
                    etText.setSelection(etText.getText().length());
                }
            }

        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    public String customFormat(String pattern, double value) {
        DecimalFormat myFormatter = new DecimalFormat(pattern);
        String output = myFormatter.format(value);
        return output;
    }

    public boolean chkConvert(String s)
    {
        String tempArray[] = s.toString().split("\\.");
        if (tempArray.length > 1) 
        {
            if (Integer.parseInt(tempArray[1]) > 0) {
                return true;
            }
            else 
                return false;
        }
        else
            return false;
    }
于 2013-08-09T10:38:16.483 回答