问题描述
我正在尝试过滤一个gridview,并且我正在编辑框中输入数据。它的效果很好,除了当我在编辑框中插入空格字符时,它以某种方式无法识别它,并且结果集为空。请帮忙
这是我的编辑器监听器:
尝试使用 trim()。希望它能为你工作。我知道你需要修改这些代码,但它可能会给你一些想法。
search .addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(可编辑的arg0){
// TODO自动生成的方法存根
()。trim());
}
@覆盖
public void beforeTextChanged(CharSequence arg0,int arg1,
int arg2,int arg3){
// TODO自动生成的方法存根
}
@Override
public void onTextChanged(CharSequence cs,int start,int before,
int count){
// TODO自动生成的方法存根
}
});
如果这不起作用,请尝试第二种选择
$ b @Override
public void afterTextChanged(Editable arg0){
String result = s。 toString()。replaceAll(,);
if(!s.toString()。equals(result)){
ed.setText(result);
ed.setSelection (result.length());
//提醒用户
}
}
@Override
public void beforeTextChanged(CharSequence arg0,int arg1,
int arg2,int arg3){
// TODO自动生成方法存根
}
@覆盖
public void onTextChanged(CharSequence cs,int start,int before,
int count){
$ b}
});
我更喜欢使用 trim()字符串文本。
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.
解决方案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.
这篇关于Textwatcher无法识别空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!