Textwatcher doesn't recognize space

微笑、不失礼 提交于 2020-01-16 00:43:24

问题


I am trying to filter a gridview and for that I am entering the data in an editbox. It works great except that when I insert a space character in editbox it somehow doesn't recognize it and the result set comes empty. Please help

Here is my editbox listener:

search.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {}

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {}

    @Override
    public void onTextChanged(CharSequence cs, int start, int before, int count) {

        MainActivity.this.adapter.getFilter().filter(cs);

        }

});

I can't figure out the problem since it's working fine with other characters.


回答1:


Try using trim(). Hope it will work for you.I know you will need to modify this code.but it may give you some idea.

search.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

        MainActivity.this.adapter.getFilter().filter(search.getText().toString().trim());

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

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

        }

});

If this doesnt work try second option

search.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
       String result = s.toString().replaceAll(" ", "");
    if (!s.toString().equals(result)) {
         ed.setText(result);
         ed.setSelection(result.length());
         // alert the user
    }

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence cs, int start, int before,
            int count) {


        }

});

I prefer first option using trim() to string text.




回答2:


Move Your filter code to afterTextChanged() method.

For example:

search.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

        MainActivity.this.adapter.getFilter().filter(search.getText().toString());

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

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

        }

});



回答3:


I was able to solve this problem with the following code:

@Override
public void afterTextChanged(Editable editable) {

    String keywords = editable.toString();
    if (keywords.length() > 0){
        String ultimo = keywords.substring(keywords.length() - 1);
        if (ultimo.equals(" ")){
            youtubeSearchView.setText(editable.toString() + "\b");
            youtubeSearchView.setSelection(keywords.length());
        }
    } 


来源:https://stackoverflow.com/questions/22706678/textwatcher-doesnt-recognize-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!