How to detect if users stop typing in EditText android

前端 未结 7 1398
长情又很酷
长情又很酷 2020-12-24 14:31

I have an EditText field in my layout. I want to perform an action when the user stops typing in that edittext field. I have implemented TextWatcher and use its functions

7条回答
  •  忘掉有多难
    2020-12-24 15:18

    I used a CountDownTimer to find out if the user stopped typing after a while. I hope to help.

    yourEditText.addTextChangedListener(new TextWatcher() {
    
            CountDownTimer timer = null;
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                if (timer != null) {
                    timer.cancel();
                }
    
                timer = new CountDownTimer(1500, 1000) {
    
                    public void onTick(long millisUntilFinished) {
                    }
    
                    public void onFinish() {
    
                        //do what you wish
    
                    }
    
                }.start();
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
            }
        });
    

提交回复
热议问题