How to comma-separate numbers in EditText

2020-06-23 08:00发布

I have an EditText that has an inputType of number. While the user is typing, I want to comma-separate the numbers. Here's a little illustration:

123 would be represented as 123

1234 would be represented as 1,234

12345 would be represented as 12,345

...and so on.

I tried adding a comma with TextWatcher as shown below:

    EditText edittext = findViewById(R.id.cashGiven);

    edittext.addTextChangedListener(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) {
            
        }

        @Override
        public void afterTextChanged(Editable editable) {
            editText.setText(separateWithComma(editText.getText().toString().trim()));
        }
    });

Pasting the separateWithComma() method here would make this question extra lengthy but then, it works: I tested it on Eclipse. I think the addTextChangedListener does not work this way because my app freezes (and then crashes much later) when I do this.

Is there a better way to achieve this? Thanks in anticipation for a positive response.

4条回答
时光不老,我们不散
2楼-- · 2020-06-23 08:17

Try to use String.format instead of what you have now.

Replace this:

editText.setText(separateWithComma(editText.getText().toString().trim()));

with this:

editText.setText(String.format("%,d", your number));

Another thing - your app may be getting this crash because every time that you are calling setText() inside afterTextChanged, another afterTextChanged is called and basically will create an infinite loop. If that is your problem you can find a good solution in here.

查看更多
男人必须洒脱
3楼-- · 2020-06-23 08:19

I've gotten two good answers but I just wanted to add this:

The reason I was facing this problem was because editText.setText(...) was being called recursively inside the afterTextChanged() function of my TextWatcher. As the previous answerers rightly stated, the solution is to temporarily stop theafterTextChanged() method from firing this way:

private boolean edit = true;
private int cursorPosition;

...
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    cursorPosition = editText.getText().toString().length() - editText.getSelectionStart();
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count){}

@Override
public void afterTextChanged(Editable editable){
    if(edit){
        edit = false;
        editText.setText(String.format("%,d", Integer.parseInt(editText.getText().toString().trim())));
        edit = true;
    }
    //setting the cursor back to where it was          
    editText.setSelection(editText.getText().toString().length() - cursorPosition);
}

This way, we have carefully meandered our way. But this poses a NEW PROBLEM:

Using this strategy, we assume that the string within the EditText can easily be translated into an int. As we input more numbers, the app would definitely crash with a NumberFormatException because of the commas. Therefore, it is important we find a way to solve this problem. Here's a method I wrote to help me:

public String getCommalessNumber(String commaNumber){

    String newNumber = "";
    String[] split = commaNumber.trim().split(",");

        for(int i = 0; i < split.length; i++){
            newNumber += split[i];
         }

    return newNumber;
}

Finally, we can do this:

boolean edit = true;

...
@Override
    public void afterTextChanged(Editable editable){
        if(edit){
            edit = false;
            editText.setText(String.format("%,d", Integer.parseInt(getCommalessNumber(editText.getText().toString().trim()))));
            edit = true;
         }
    }

I hope this helps someone out there. Merry coding!!

查看更多
倾城 Initia
4楼-- · 2020-06-23 08:24

Try this code:

et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                et.removeTextChangedListener(this);

                try {
                    String givenstring = s.toString();
                    Long longval;
                    if (givenstring.contains(",")) {
                        givenstring = givenstring.replaceAll(",", "");
                    }
                    longval = Long.parseLong(givenstring);
                    DecimalFormat formatter = new DecimalFormat("#,###,###");
                    String formattedString = formatter.format(longval);
                    et.setText(formattedString);
                    et.setSelection(et.getText().length());
                    // to place the cursor at the end of text
                } catch (NumberFormatException nfe) {
                    nfe.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                et.addTextChangedListener(this);

            }
        });

See this post

查看更多
一夜七次
5楼-- · 2020-06-23 08:28

For those that might want to use this approach, change the return type of getCommalessNumber(String commaNumber) method to String instead of int.

查看更多
登录 后发表回答