Textwatcher不承认空间(Textwatcher doesn't recognize

2019-10-19 16:27发布

我试图筛选GridView和为我在编辑框中输入数据。 当我在编辑框中插入空格字符它在某种程度上不承认它,结果集都空它的工作原理,只是巨大的。 请帮忙

这里是我的编辑框监听器:

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);

        }

});

因为它的正常工作与其他人物,我不能找出问题。

Answer 1:

尝试使用trim() 希望它会工作为你。我知道你会需要修改这个code.but它可能给你一些想法。

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

        }

});

如果这不起作用尝试第二个选项

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) {


        }

});

我喜欢用第一个选项trim()字符串文本。



Answer 2:

将你的过滤器代码afterTextChanged()方法。

例如:

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

        }

});


Answer 3:

我可以用下面的代码来解决这个问题:

@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());
        }
    } 


文章来源: Textwatcher doesn't recognize space