how to get the events of searchview in android

后端 未结 6 890
傲寒
傲寒 2020-12-15 04:58

I\'m using a search view in my application. Now i just want to get the text typed in the SearchView text box and display it on another textview. If i typed the text and clic

6条回答
  •  生来不讨喜
    2020-12-15 05:18

    It also can be done with RXAndroid and RxBinding by Jake Wharton like this:

    RxSearchView.queryTextChanges(searchView)
                .debounce(500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if(charSequence!=null){
                            // Here you can get the text
                            System.out.println("SEARCH===" + charSequence.toString());
                        }
                    }
                });
    

    Code is subscribing to observe text change with some delay of 500 milliseconds.

    This is a link to git repo to get RXAndroid: https://github.com/JakeWharton/RxBinding

提交回复
热议问题