How to trigger an action when user has paused typing in android edittext?

江枫思渺然 提交于 2019-12-11 09:02:00

问题


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
            }
        };
});


来源:https://stackoverflow.com/questions/20897472/how-to-trigger-an-action-when-user-has-paused-typing-in-android-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!