DecimalFormat problem throws a StackOverFlowError?

I have text editing. I want to enter decimal values ​​into it.

that is, when I enter the first number. It should look like this:.01

then I enter the second number. it should be like this:.12

then for the third. It should look like this:1.23

it will go like this ... how to implement this scenario on the Android platform . Any idea?

Edit:

I have tried this. But I got a StackOverFlowError when I add a line to EditText. I posted my code below:

amount.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                try {
                    if (s.length() == 1) {
                        Float f = Float.parseFloat(s.toString()) / 100;
                        System.out.println(String.valueOf(f));
                        amount.setText("");
                        amount.append(String.valueOf(f));//getting stackoverflow error here
                    } else if (s.length() > 1) {
                        String str = s.toString().replace(".", "");
                        Float f = Float.parseFloat(str) / 100;
                        System.out.println(String.valueOf(f));
                        amount.setText("");
                        amount.append(String.valueOf(f));
                    }
                } catch (Exception e) {

                }
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
            }
        });  
+3
source share
1 answer

Look, you are implementing a TextChangedKListener for the element in which you are changing the text of the element.

So in these lines

amount.setText("");
amount.append(String.valueOf(f))

TextWatcher ( ).

, API- API afterTextChanged :

, ,

afterTextChanged API TextWatcher :

, , afterTextChanged() , , . , , setSpan (Object, int, int, int) onTextChanged (CharSequence, int, int, int), , , .

+1

Source: https://habr.com/ru/post/1790633/


All Articles