Android 在第一个 Activity 开始时显示软键盘?
我需要在应用程序启动时显示虚拟键盘,但到目前为止我失败了。
我在方法“OnCreate”中使用此代码来显示虚拟键盘,
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(txtBuscar.getId(), InputMethodManager.SHOW_FORCED);
此代码在任何时间任何屏幕上都可以正常工作,但在“第一个”活动开始时不起作用。为什么?
当我开始另一个活动时我尝试了它并且它有效,但当我开始“第一个”活动时它不起作用。
我尝试将此代码放入事件“OnCreate”等中......但它似乎不起作用。
当我启动应用程序时是否有“强制”显示键盘?
提前致谢。
I need to display the virtual keyboard when the application starts, but so far I've failed.
I use this code in method "OnCreate"to display the virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(txtBuscar.getId(), InputMethodManager.SHOW_FORCED);
this code works fine on any screen at any time, but does not work when the "first" activity begins. Why?
I tried it when I start another activity and it works, but does not work when I start the "first"activity.
I tried to put this code in the events "OnCreate"and many more .... but it seems not work.
is there anyway to "force" to display the keyboard when I start the application?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了解决方案:
谢谢!
I Found the solution:
Thanks !!!
如果该 Activity 首次从后台启动,则不会调用 onCreate。您是否尝试过将该代码放入 onResume 中?
仅当 Activity 首次启动或 Activity 被终止并且用户再次导航到该 Activity 时,才会调用 onCreate。因此,如果 Activity 仍处于活动状态但处于后台,则它不会调用 onCreate。
另一方面,每次 Activity 从后台转到前台(在屏幕上可见)时,都会调用 onResume。
如果您有兴趣,这里是活动生命周期的链接 http://developer.android。 com/reference/android/app/Activity.html。
希望有帮助。
onCreate will not be called if the activity is first brought from background. Have you tried put that code in onResume?
onCreate is called only when activity is first start or the activity is killed and user navigate to the activity again. So if the activity is still alive but in background, it will not call onCreate.
On the other hand, onResume will be called every time the activity comes to foreground (visible on screen) from background.
Here is link to activity life cycle if you are interested http://developer.android.com/reference/android/app/Activity.html.
Hope it helps.
我遇到了同样的问题,下面的方法对我有帮助
I faced with the same issue, this method below helped me
如今,我们有另一种显示键盘的方法。它是WindowInsetsControllerCompat。这也适用于活动开始。
在 onCreate 中,您应该使用:
另请参阅:
https://developer.android。 com/develop/ui/views/touch-and-input/keyboard-input/visibility#ShowReliously
Nowadays, we have an alternative for showing keyboard. It is
WindowInsetsControllerCompat
. This works at activity start also.In onCreate, you should use:
See also:
https://developer.android.com/develop/ui/views/touch-and-input/keyboard-input/visibility#ShowReliably