EditText TextChangeListener issue

2019-05-07 03:43发布

问题:

I have an edit text meant for searching purpose. I have added

searchET.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub              
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub              
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                //intent to another page where i show my search result
            }
        });

The problem is: When I give a search string for eg: "fort" i get the required result in the next page. But when I press the back button, it doesnt move to the previous page on its first click. I will have to press back button 4 times to goto the previous page. This is because my search string is of length 4 and each time a value is entered into the edittext, the textchangelistener is called. How can I solve this issue? Please reply. Thanks in advance.

回答1:

Depending on what you need, you can go to the next page based on some condition. For example, start a timer, and if the afterTextChanged is called before the timer expires, reset the timer. Alternatively, you can have a button 'Search' where the user explicitly indicates that he's done typing the word.

If you can share the required behaviour, better alternatives can be suggested.



回答2:

actually when you want to decide when the text ends there is no point of including an addTextChangedListener. To improve the UI you could add this button into ur edit text .refer this : edittext with view