Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

addTextChangedListener onTextChanged count Not working On android version 10

I use addTextChangedListener to search item from server with retrofit. but only android version 10 onTextChanged count not working...?

Here is my code

searchEdit.addTextChangedListener(new TextWatcher() {
        @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 (count>1) {
                String name = searchEdit.getText().toString().trim();

                if (!name.isEmpty()) {
                    searchItemByName(name);
                }
            }
        }
        @Override
        public void afterTextChanged(Editable s) {
            
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'll go with the assumption that you're using count parameter in onTextChanged callback as the number of characters inside your searchEdit Edittext.

Solution: You need to use s.length() function instead of the parameter count, like this:

searchEdit.addTextChangedListener(new TextWatcher() {

            @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 (s.length()>1) {
                    String name = searchEdit.getText().toString().trim();

                    if (!name.isEmpty()) {
                        searchItemByName(name);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

Explanation:

If you have a look at the documentation of android.text.TextWatcher::onTextChanged function below:

/**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * have just replaced old text that had length <code>before</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     */
    public void onTextChanged(CharSequence s, int start, int before, int count);

it says that count parameter refers to the number of characters changed in s, and thus, if you're typing into the edittext your onTextChanged function will be called with the parameter count is 1.

Alternatively: you can use afterTextChanged callback with s.length().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...