简体   繁体   中英

TextWatcher overrides not called when we backspace remove last character. How do I know if the last character is removed?

I'm trying to save what's being typed on an EditText. When the user types some text, text watcher works fine. When the user removes text, the text watcher works fine until the user removes the last character.

    answerEditText.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) {
            saveAnswer(editable.toString())
        }
    });

What is expect saveAnswer to get when the last character is removed is an empty string. But the actual result is that it is not triggered.

Use TextWatcher and onTextChange method to track user input, when user write something inside watcher first time, then yourself store the text and keep checking diff yourself. Since onTextChanged is fired everytime when user changes the character.

String userInputText=userText.getString().toString();

    @Override 
    onTextChanged(CharSequence s, int start, int before, int count){
        compareText(s, userInputText); //compare and do whatever you need to do
        previousText = s;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM