Clear EditText text after adding onTextChanged implemenation

后端 未结 5 723
孤街浪徒
孤街浪徒 2021-01-11 11:18

I\'m adding a listener to an EditText field to appropriately format the number to currency in real time.

        loanText.addTextChangedListener(new TextWatc         


        
5条回答
  •  情歌与酒
    2021-01-11 11:50

    The problem is that your TextWatcher sees that you are "clearing" the text, and therefore sends off a request to its callback methods(afterTextChanged, beforeTextChanged, etc).

    What i've done is to simply remove the TextWatcher, clear the text, and add the TextWatcherback to the EditText. That way, nothing is listening to my EditText changes.

    You will probably have to hold on to an instance of the TextWatcher instead of inlining it like you did. That way you don't have to create one every time you want to clear the EditText

    1. loanText.removeTextChangedListener(yourTextWatcherObject);
    2. loanText.setText("");
    3. loanText.addTextChangedListener(yourTextWatcherObject);

提交回复
热议问题