启动 Activity 时自动弹出键盘
我有一个相对简单的问题。我有一个活动,其中有很多 EditText。当我打开活动时,它会自动聚焦到第一个 EditText 并显示虚拟键盘。
我怎样才能防止这种情况发生?
I got a relative simple question. I have an activity with a lot of EditText's in them. When I open the activity it automatically focusses to the first EditText and displays the virtual keyboard.
How can I prevent this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
在 XML 文件的布局标记中使用此属性:
正如其他成员在评论中所报告的,它不适用于
ScrollView
,因此您需要将这些属性添加到ScrollView
的主要子级中代码>.Use this attributes in your layout tag in XML file:
As reported by other members in comments it doesn't works on
ScrollView
therefore you need to add these attributes to the main child ofScrollView
.您可以将其添加到您的 Android Manifest 活动中:
You can add this to your Android Manifest activity:
我在这里描述了几个实现,但现在我已将属性添加到我的
Activity
的AndroidManifest.xml
中:我认为即使您使用 <代码>片段。
I have several implementations described here, but now i have added into the
AndroidManifest.xml
for myActivity
the property:I think this is the easy way even if you are using
fragments
.如果您的 Activity 有其他视图(例如
ListView
),您还可以:在 onResume() 中从
editText
获取焦点。我知道这个问题已经得到解答,但只是提供了一个对我有用的替代解决方案:)
If you have another view on your activity like a
ListView
, you can also do:in your onResume() to grab focus from the
editText
.I know this question has been answered but just providing an alternative solution that worked for me :)
在您的活动代码中使用它:
Use this in your Activity's code:
https://stackoverflow.com/a/11627976/5217837 这几乎是正确的:
但它应该是 SOFT_INPUT_STATE_HIDDEN 而不是 SOFT_INPUT_STATE_ALWAYS_VISIBLE
https://stackoverflow.com/a/11627976/5217837 This is almost correct:
But it should be SOFT_INPUT_STATE_HIDDEN rather than SOFT_INPUT_STATE_ALWAYS_VISIBLE
我遇到了一个类似的问题,即使在平板电脑上使用 Android 3.2.1 切换选项卡时,键盘也会自动弹出并保持打开状态。使用以下方法:
在每个 EditText 活动的 onCreate() 和 onPause() 中:
setEditTextFocus(myEditText, false);
对于每个 EditText 一个 OnTouchListener:
对于每个
EditText<
OnEditorActionListener
中的 /code>:对于
layout xml
中的每个EditText
:可能还有更多的代码优化可能。
I had a simular problem, even when switching tabs the keyboard popped up automatically and stayed up, with Android 3.2.1 on a Tablet. Use the following method:
In the onCreate() and in the onPause() of the activity for each EditText:
setEditTextFocus(myEditText, false);
For each EditText an OnTouchListener:
For each
EditText
in theOnEditorActionListener
:And for each
EditText
in thelayout xml
:There is probably more code optimizing possible.
我找到了这个对我有用的简单解决方案。在父布局中设置这些属性:
现在,当活动启动时,此主布局将默认获得焦点。
此外,我们可以通过再次将焦点赋予主布局来在运行时从子视图中删除焦点,如下所示:
Hope it will work for you 。
I have found this simple solution that worked for me.Set these attributes in your parent layout:
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime by giving the focus to the main layout again, like this:
Hope it will work for you .
这是我正在使用的解决方案,不是最好的解决方案,但它对我来说效果很好
this is the solution I am using, is not the best solution but it's working well for me
有趣的是,本文档 https://developer.android.com/training/keyboard-input /visibility.html 声明当活动启动并将焦点赋予文本字段时,不会显示软键盘(然后继续向您展示如何显示键盘(如果您想使用一些代码) )。
在我的三星 Galaxy S5 上,这就是我的应用程序(没有清单条目或特定代码)的工作方式 - 没有软键盘。然而,在 Lollipop AVD 上,显示软键盘——这与上面给出的文档相矛盾。
如果您在 AVD 中测试时遇到此行为,您可能需要在真实设备上进行测试,看看会发生什么。
Interestingly, this documentation https://developer.android.com/training/keyboard-input/visibility.html states that when an activity starts and focus is given to a text field, the soft keyboard is not shown (and then goes on to show you how to have the keyboard shown if you want to with some code).
On my Samsung Galaxy S5, this is how my app (with no manifest entry or specific code) works -- no soft keyboard. However on a Lollipop AVD, a soft keyboard is shown -- contravening the doc given above.
If you get this behavior when testing in an AVD, you might want to test on a real device to see what happens.
在以下帖子中有一些很好的答案:阻止 EditText 在 Activity 启动时获得焦点。我经常使用的是 Morgan 编写的以下代码:
注意: 虚拟项目必须放置在可聚焦元素之前。
我认为即使与 ScrollView 一起使用它也应该完美地工作,并且在可访问性方面也没有任何问题。
This has some good answers at the following post : Stop EditText from gaining focus at Activity startup. The one I regularly use is the following code by Morgan :
NOTE : The dummy item has to be PLACED RIGHT BEFORE the focusable element.
And I think it should work perfectly even with ScrollView and haven't had any problems with accessibility either for this.
当您的 EditText 在活动开始时自动获得焦点时,就会发生这种情况。因此,解决此问题的一种简单而稳定的方法是将初始焦点设置到任何其他视图,例如按钮等。
您可以在布局 XML 中执行此操作,无需任何代码。
This occurs when your EditText automatically gets Focus as when you activity starts. So one easy and stable way to fix this, is simply to set the initial focus to any other view, such as a Button etc.
You can do this in your layout XML, no code required..
接受的答案对我不起作用,这就是为什么给出答案工作解决方案,可能会有帮助!
现在键盘已打开,请享受:)
Accepted answer is not working for me, that's why give answer working solution, may be it is helpful !
Now keyboard is open enjoy :)
工作正常
Working fine
将以下代码添加到活动 XML 的顶部,并确保视图位于 EditText 之上
Add below code to your top of the activity XML and make sure the View is above EditText
android:focusableInTouchMode="true"
将以上行添加到具有焦点并导致
softInputKeyboard< 的
EditText
或TextInputLayout
的 xml 中/code> 弹出。这为我们解决了问题,现在键盘不会弹出
android:focusableInTouchMode="true"
Add the above line to xml of
EditText
orTextInputLayout
which has focus and is causing thesoftInputKeyboard
to pop up.This solved the problem for us and now the keyboard doesn't popup
search_edit_text = (EditText) findViewById(R.id.search_edit_text);
这对我有用,片段可能有一些不同的语法。 这适用于活动
search_edit_text = (EditText) findViewById(R.id.search_edit_text);
This works for me guys fragment may have some different syntax . THIS WORKS FOR ACTIVITY
在您的活动代码中使用它:
Use this in your Activity's code:
如果您的视图有 EditText 和 Listview,则默认情况下将打开键盘。
要隐藏默认情况下弹出的键盘,请执行以下操作:
确保在获取 editText 视图后请求将焦点放在列表视图上。
例如
,如果您这样做,则 editText 将获得焦点并且键盘将弹出。
If your view has EditText and Listview then Keyboard will open up by default.
To hide keyboard from popping up by default do the following
Make sure you are requesting focus on listview after getting view for editText.
For e.g.
If you do it, then editText will get focus and keyboard will pop up.