在普通的Edittext
中,我会在TextWatcher
方法中使用类似的方法:
@Override
public void onTextChanged(CharSequence charSequence, int start, int before,
int count) {
String str = charSequence.toString();
//if it starts with " " then don't recognise it, simply put ""
if (str.equals(" ")) {
Edittext.setText("");
}
}
在searchview
中我该如何做?
我正在尝试类似的事情:
// listening to search query text change
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String query) {
String str = searchView.getQuery().toString();
//if it starts with " " then don't recognise it, make it ""
if (str.equals(" ")) {
searchView.setQuery("", false);
}
}
});
答案 0 :(得分:0)
也许
@Override
public boolean onQueryTextChange(String query) {
String str = searchView.getQuery().toString().trim();
//if it starts with " " then don't recognise it, make it ""
if (str.equals("")) {
searchView.setQuery("", false);
}
}
});
答案 1 :(得分:0)
SearchView 是一个 LinearLayout,所以在它里面找到一个 EditText 并设置 textChangedListener:
fun SearchView.ignoreLeadingWhiteSpaces(){
findViewById<EditText>(androidx.appcompat.R.id.search_src_text).apply {
doOnTextChanged { text, _, _, _ ->
if(text?.startsWith(" ") == true) {
setText(text?.trim())
}
}
}
}
调用:
searchView.ignoreLeadingWhiteSpaces()