从另一个活动返回时,SearchView会获得焦点并打开键盘 [英] SearchView gains focus and opens keyboard when returning from another activity
问题描述
当我从另一个活动返回到主要活动时,searchView获得了焦点,并且键盘打开了,我设法使用以下命令阻止了键盘的打开:
When I return from another activity to the main activity, the searchView gains focus and the keyboard opens, I managed to stop the keyboard from opening by using:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
但这还不够,如果打开并关闭了操作栏下拉菜单,则键盘会再次出现,因为searchView仍然具有焦点(请记住我只关闭了键盘).我试图获取对searchView的引用,并使它失去焦点,但这没有用.
but this is not sufficient, if the actionbar drop down menu is opened and closed the keyboard reappears, because the searchView still has focus (remember I only closed the keyboard). I have tried to get a reference to the searchView and make it lose focus, but this has not worked.
因此,基本上,我只希望searchView永远不要聚焦或提示键盘打开,除非真正触摸了searchView文本区域.
So basically, I just want the searchView to never have focus or prompt the keyboard to open, unless the searchView text area is actually touched.
推荐答案
这是由于SearchView
是将活动重新带回视图时的第一个可聚焦视图-相同的问题
This is due to the SearchView
being the first focusable view when the activity is brought back into view - the same issue can be seen for EditText
s.
解决方案相同-将层次结构中的较早视图设置为可聚焦.就我而言,这是Toolbar
我的SearchView
位于以下位置:
The solution is the same - set an earlier view in the hierarchy to focusable. In my case, this was the Toolbar
my SearchView
was on top of:
<android.support.v7.widget.Toolbar
style="@style/ToolbarStyle"
android:focusableInTouchMode="true"
android:id="@+id/search_activity_toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"/>
<android.support.v7.widget.SearchView
android:iconifiedByDefault="false"
... />
现在,当我进入活动(clearFocus(); requestFocus(...);
)时,我可以专注于我的SearchView
,然后当您通过搜索结果退出活动,然后通过按Back返回时,键盘处于与您输入时相同的状态左.
Now I can gain focus on my SearchView
when entering the activity (clearFocus(); requestFocus(...);
), then when you exit the activity via a search result, then return by pressing Back, the keyboard is in the same state as when you left.
请注意,有关链接问题的其他一些解决方案,即在活动"上设置windowSoftInputMode="stateUnchanged"
确实完成了不为我工作.
Note that some of the other solutions on the linked question, namely setting windowSoftInputMode="stateUnchanged"
on the Activity did not work for me.
这篇关于从另一个活动返回时,SearchView会获得焦点并打开键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!