如何理解像 whatsapp for EditText 一样停止和开始输入?

How to understand stop and start typing like whatsapp for EditText?

我有 edittext,我想了解停止并开始输入。我在 TextChanged 上听了 textwatcher,我使用定时器来打字。

但是当edittext的文本不为空时,实际的打字操作理解不正确

我想看:

My edittext text:

--ad-- --> typing...

--ads-- --> typing...

--ads-- --> after 900 ms stop typing . ::: but not understand

TextWatcher textWatcher = new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

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

        public void onTextChanged(CharSequence s, int start, final int before, final int count) {

            if (count != 0 && count >= before) {
                typingTimer.startTyping();
                return;
            }

            typingTimer.stopTyping();

        }
    }; 

事实上你需要一个计时器。

TextWatcher textWatcher = new TextWatcher() {

    private Timer timer = new Timer();
    private final long TYPING_DELAY = 900; // milliseconds

    public void afterTextChanged(Editable s) {
    }

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

    public void onTextChanged(CharSequence s, int start, final int before, final int count) {

        // do what you want
        // show "is typing"

        timer.cancel();
        timer = new Timer();
        timer.schedule(
            new TimerTask() {
                @Override
                public void run() {
                    // here, you're not typing anymore
                }
            }, 
            TYPING_DELAY
        );

    }
};