如何在 Android SearchView 中关闭键盘?
我在 ActionBar 中有一个 searchView 。我想在用户完成输入后关闭键盘。我在 searchView 上有以下 queryTextListener
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
showProgress();
// Do stuff, make async call
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return true;
}
};
基于类似的问题,以下代码应该关闭键盘,但在这种情况下不起作用:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
我也尝试过:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
两者都不起作用。我不确定这是否是 Honeycomb 特定的问题,或者是否与 ActionBar 中的 searchView 有关,或两者兼而有之。有谁让它工作或知道为什么它不起作用?
I have a searchView in the ActionBar. I want to dismiss the keyboard when the user is done with input. I have the following queryTextListener on the searchView
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
showProgress();
// Do stuff, make async call
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return true;
}
};
Based on similar questions, the following code should dismiss the keyboard, but it doesn't work in this case:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I've also tried:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
Neither one works. I'm not sure if this is a Honeycomb specific problem or if it's related to the searchView in the ActionBar, or both. Has anyone gotten this working or know why it does not work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
我正在尝试做类似的事情。我需要从另一个
Activity
启动SearchActivity
,并在加载时让搜索词出现在打开的搜索字段中。我尝试了上面的所有方法,但最后(类似于 Ridcully 的答案上面)我将一个变量设置为SearchViewonCreateOptionsMenu()
中调用onCreateOptionsMenu()
,然后在onQueryTextSubmit()
中调用clearFocus()
当用户提交新搜索时SearchView
:I was trying to do something similar. I needed to launch the
SearchActivity
from anotherActivity
and have the search term appear on the opened search field when it loaded. I tried all the approaches above but finally (similar to Ridcully's answer above) I set a variable toSearchView
inonCreateOptionsMenu()
and then inonQueryTextSubmit()
calledclearFocus()
on theSearchView
when the user submitted a new search:简单、开门见山、干净:
Simple, straight to the point and clean:
只需在 onQueryTextSubmit 上返回 false 即可,如下所示
just return false on onQueryTextSubmit just like below
对我来说,以上所有内容都不适用于第一次提交。它被隐藏起来,然后立即重新显示键盘。我必须将
clearFocus()
发布到视图的处理程序上,以使其在完成其他所有操作后发生。For me, none of the above was working on the first submit. It was hiding and then immediately re-showing the keyboard. I had to post the
clearFocus()
on the view's handler to make it happen after it was done with everything else.如果你打电话
然后然后就可以了
Somehow it works if you call
and then
对我来说,以下工作有效:
在我的活动中,我有一个成员变量
在
onCreateOptionsMenu()
中,我像这样设置该变量:最后在
QueryTextListener
中,我这样做:我查看了SearchView的源代码,如果您不将查询文本重置为空字符串,SearchView就会这样做,并且也不会删除键盘。实际上,在源代码中深入研究,结果是一样的,yuku 建议,但我仍然更喜欢我的解决方案,因为我不必搞乱那些低级的东西。
For me, the following works:
In my activity I have a member variable
In
onCreateOptionsMenu()
I set that variable like so:In the
QueryTextListener
at last, I do this:I had a look at the source code of SearchView, and if you do not reset the query text to an empty string, the SearchView just does that, and does not remove the keyboard neither. Actually, drilling down deep enough in the source code, it comes to the same, yuku suggested, but still I like my solution better, as I do not have to mess around with those low level stuff.
编辑:我在顶部添加了更好的解决方案,但也保留了旧答案作为参考。
原始答案:我使用 setOnQueryTextListener 进行编程。当搜索视图隐藏时,键盘消失,然后当它再次可见时,键盘不会弹出。
Edit: I added the better solution on top, but also kept the old answer as a reference.
Original Answer: I programmed using a setOnQueryTextListener. When the searchview is hidden the keyboard goes away and then when it is visible again the keyboard does not pop back up.
如果有人正在寻找如何折叠 searchView/键盘,请使用下面的代码
if someone is looking how to collpase searchView/keyboard, use below code
在我正在处理双窗格活动的平板电脑应用程序中,我只编写了
f.getView().requestFocus(); // f 是搜索的目标片段
,这足以在搜索后关闭软键盘。无需使用InputMethodManager
In a tablet app I'm working on with a dual pane activity, I've wrote only
f.getView().requestFocus(); // f is the target fragment for the search
and that was enough to dismiss the soft keyboard after a search. No need to use InputMethodManager
我使用 ActionBarSherlock 4.3 并且有一个 ProgressDialog。当我在 postExecute 方法中关闭它时,Searchview 获得焦点。解决这个问题:
希望有帮助。
I used ActionBarSherlock 4.3 and I have a ProgressDialog. When I dismiss it in postExecute method, Searchview gain focus. To fix that:
Hope it helps.
你可以使用它。
You can use it.
如果您是 kotlin 开发人员,则隐藏或显示软键盘的最简单方法是创建一个简单的 Kotlin 扩展函数 并添加以下代码
对于 kotlin 开发人员
}
对于 Java 开发人员(java没有扩展功能)因为它纯粹是oop的语言
}
The easiest and the simple way to hide or show the soft keyboard if you are kotlin developer create a simple Kotlin extension function and add this following code
For kotlin developer's
}
For Java Developer (java has a no extension function) because it is purely oop's language
}
两个对我有用的解决方案,第一个使用 SearchView 实例:
第二个解决方案:
Two solutions that worked for me, the first one using the SearchView instance:
Second solution:
下面的代码可以帮助我在 Searchview 中隐藏键盘
Below Code is working for me to hide keyboard in Searchview
你为什么不试试这个呢?当您触摸 X 按钮时,无论如何都会触发搜索视图的焦点。
Why dont you try this? When you touch the X button, you trigger a focus on the searchview no matter what.
我尝试了上面所有的答案,但没有一个起作用,清除对 Resume 的关注对我有用
I tried all the answers above but none worked, clearing focus onResume worked for me