How to detect if users stop typing in EditText android

前端 未结 7 1306
长情又很酷
长情又很酷 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:16

    There is a very simple and easy way of doing this using RxBindings for Android,

    Get the library if you haven't already,

    compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
    

    Add and modify this snippet to your needs,

    RxTextView.textChanges(mEditText)
                .debounce(3, TimeUnit.SECONDS)
                .subscribe(textChanged -> {
                    Log.d("TAG", "Stopped typing!");
                });
    

    What is Debounce Operator? - Learn more about it here

    In our context, debounce will only notify you of the text change inside the EditText after a certain time has passed from the last text change that occurred inside the EditText.

    Here I am waiting for 3 seconds until the last text change happened to conclude that the user has stopped typing. You can modify it according to your needs.

提交回复
热议问题