在 SearchView 中处理物理键盘的 Enter 键 [英] Handling physical keyboard's Enter key in SearchView
问题描述
我在我的应用程序中实现了一个 SearchView,当我在使用软键盘(使用
现在我打算使用物理键盘让我的用户能够使用该应用程序,但我不知道如何从物理键盘触发搜索事件(onQueryTextChange 没有被调用)钥匙).当我在 Android Studio 中运行模拟器时会发生同样的事情.我试过了:
viewHolder.mTextSearch.setOnKeyListener(new View.OnKeyListener() {@覆盖public boolean onKey(View view, int i, KeyEvent keyEvent) {Log.d(TAG, "查询:" + keyEvent);返回假;}});
但是 SearchView 似乎忽略了任何键的侦听器...似乎 QueryTextListener 是处理搜索的唯一方法,但是如何处理特殊字符呢?我是否应该将其切换到更灵活的 EditText?
有什么想法吗?
我是这样处理的:
//从 SearchView 中获取编辑文本EditText editText = (EditText) viewHolder.mTextSearch.findViewById(android.support.v7.appcompat.R.id.search_src_text);editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {@覆盖public boolean onEditorAction(TextView v, int keyAction, KeyEvent keyEvent) {如果 (//软键盘搜索keyAction == EditorInfo.IME_ACTION_SEARCH ||//物理键盘回车键(keyEvent != null && KeyEvent.KEYCODE_ENTER == keyEvent.getKeyCode()&&keyEvent.getAction() == KeyEvent.ACTION_DOWN)) {新的 SearchForEquipmentTask(false, viewHolder.mTextSearch.getQuery().toString()).execute();返回真;}返回假;}});
感谢您的帮助!
另见:
I've implemented a SearchView in my application which is working properly when I hit the search button while I'm using the soft keyboard (using the QueryTextListener):
viewHolder.mTextSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
new SearchForEquipmentTask(false, viewHolder.mTextSearch.getQuery().toString()).execute();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
//Log.d(TAG, "Query: " + newText);
return false;
}
});
Now I'm planning to use a physical keyboard for my users to be able to work with the app, but I can't figure out how to fire the search event from the physical keyboard (onQueryTextChange is not getting invoked with that key). Same thing happens when I'm running the emulator in Android Studio. I've tried:
viewHolder.mTextSearch.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
Log.d(TAG, "Query: " + keyEvent);
return false;
}
});
But the SearchView seems to ignore the listener for any key... It seems that QueryTextListener is the only way to handle searches, but how to deal with special characters? Should I switch it to EditText which is more flexible?
Any idea?
That's how I've handled it:
//Grab de EditText from the SearchView
EditText editText = (EditText) viewHolder.mTextSearch
.findViewById(android.support.v7.appcompat.R.id.search_src_text);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int keyAction, KeyEvent keyEvent) {
if (
//Soft keyboard search
keyAction == EditorInfo.IME_ACTION_SEARCH ||
//Physical keyboard enter key
(keyEvent != null && KeyEvent.KEYCODE_ENTER == keyEvent.getKeyCode()
&& keyEvent.getAction() == KeyEvent.ACTION_DOWN)) {
new SearchForEquipmentTask(false, viewHolder.mTextSearch
.getQuery().toString()).execute();
return true;
}
return false;
}
});
Thanks for your help!
See also:
- Grab the EditText element from a SearchView
- Android: how to make keyboard enter button say "Search" and handle its click?
这篇关于在 SearchView 中处理物理键盘的 Enter 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!