How to trigger an action when user has paused typi

2019-07-25 16:45发布

问题:

How to trigger an action when user has paused typing for some time in EditText?

回答1:

You can do this:

final Handler mHandler = new Handler();
editText.addTextChangedListener(new TextWatcher() {

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

        }

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

        @Override
        public void afterTextChanged(Editable s) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler.postDelayed(userStoppedTyping, 2000); // 2 second
        }

        Runnable userStoppedTyping = new Runnable() {

            @Override
            public void run() {
                // user didn't typed for 2 seconds, do whatever you want
            }
        };
});