EditText - 如何检测键入 3 个或更多字符并执行搜索
EditText - How to detect typing 3 or more characters and perform search
需要帮助。已经 4 天没有任何反应。
尝试进行 SQLite 数据库搜索。
如何使列表视图不立即显示,而仅在用户输入搜索查询时显示?
也就是说,数据库中的数据未被过滤,但当用户输入请求时,匹配项出现在列表视图中。
提前谢谢大家了!`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
userFilter = findViewById(R.id.userFilter);
listView = findViewById(R.id.listView);
final DatabaseAccessSmeta databaseAccessSmeta = DatabaseAccessSmeta.getInstance(this);
databaseAccessSmeta.open();
List<String> quotes = databaseAccessSmeta.Search();
databaseAccessSmeta.close();
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.region_list, quotes);
this.listView.setAdapter(adapter);
adapter.getFilter().filter(userFilter.getText().toString());
userFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) { }
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
});
}`
如果你只想在用户按下enter/search键后加载结果,那么不需要添加TextWatcher
,只需添加EditorActionListener:
/* Adding action listener for handling search click */
userFilter.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Hiding keyboard
userFilter.clearFocus();
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (in != null) {
in.hideSoftInputFromWindow(userFilter.getWindowToken(), 0);
}
// Initiate search
adapter.getFilter().filter(userFilter.getText().toString());
return true;
}
return false;
});
同样在您的 xml 中,将 android:imeOptions="actionSearch"
添加到您的 EditText
。
编辑:
如果你想让它在输入3个字符后可见,那么你必须添加TextWatcher
:
userFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String query = (s != null) ? s.toString() : "";
if (query.length() >= 3) {
listView.setVisibility(View.VISIBLE);
adapter.getFilter().filter(query);
} else {
// Either set an empty list or change visibility
listView.setVisibility(View.INVISIBLE);
adapter.getFilter().filter("");
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void onTextChanged(CharSequence s, int start, int before, int count) { }
});
需要帮助。已经 4 天没有任何反应。 尝试进行 SQLite 数据库搜索。 如何使列表视图不立即显示,而仅在用户输入搜索查询时显示? 也就是说,数据库中的数据未被过滤,但当用户输入请求时,匹配项出现在列表视图中。 提前谢谢大家了!`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
userFilter = findViewById(R.id.userFilter);
listView = findViewById(R.id.listView);
final DatabaseAccessSmeta databaseAccessSmeta = DatabaseAccessSmeta.getInstance(this);
databaseAccessSmeta.open();
List<String> quotes = databaseAccessSmeta.Search();
databaseAccessSmeta.close();
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.region_list, quotes);
this.listView.setAdapter(adapter);
adapter.getFilter().filter(userFilter.getText().toString());
userFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) { }
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
});
}`
如果你只想在用户按下enter/search键后加载结果,那么不需要添加TextWatcher
,只需添加EditorActionListener:
/* Adding action listener for handling search click */
userFilter.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Hiding keyboard
userFilter.clearFocus();
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (in != null) {
in.hideSoftInputFromWindow(userFilter.getWindowToken(), 0);
}
// Initiate search
adapter.getFilter().filter(userFilter.getText().toString());
return true;
}
return false;
});
同样在您的 xml 中,将 android:imeOptions="actionSearch"
添加到您的 EditText
。
编辑:
如果你想让它在输入3个字符后可见,那么你必须添加TextWatcher
:
userFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String query = (s != null) ? s.toString() : "";
if (query.length() >= 3) {
listView.setVisibility(View.VISIBLE);
adapter.getFilter().filter(query);
} else {
// Either set an empty list or change visibility
listView.setVisibility(View.INVISIBLE);
adapter.getFilter().filter("");
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void onTextChanged(CharSequence s, int start, int before, int count) { }
});