当用户在android edittext中暂停键入时如何触发操作?

时间:2014-01-03 05:56:03

标签: android android-edittext

如果用户在EditText中暂停键入一段时间后如何触发操作?

1 个答案:

答案 0 :(得分:7)

你可以这样做:

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