Android 在第一个 Activity 开始时显示软键盘?

发布于 2024-10-20 21:44:02 字数 463 浏览 4 评论 0原文

我需要在应用程序启动时显示虚拟键盘,但到目前为止我失败了。

我在方法“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

面犯桃花 2024-10-27 21:44:02

我找到了解决方案:

txtPassword.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager keyboard = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.showSoftInput(txtPassword, 0); 
            }
        },200);

谢谢!

I Found the solution:

txtPassword.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager keyboard = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.showSoftInput(txtPassword, 0); 
            }
        },200);

Thanks !!!

腹黑女流氓 2024-10-27 21:44:02

如果该 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.

亚希 2024-10-27 21:44:02

我遇到了同样的问题,下面的方法对我有帮助

public static void showKeyboard(Context context) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm != null) {
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
}

I faced with the same issue, this method below helped me

public static void showKeyboard(Context context) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm != null) {
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
}
安稳善良 2024-10-27 21:44:02

如今,我们有另一种显示键盘的方法。它是WindowInsetsControllerCompat。这也适用于活动开始。

在 onCreate 中,您应该使用:

editText.requestFocus()
WindowCompat.getInsetsController(window, editText)!!.show(WindowInsetsCompat.Type.ime())

另请参阅:
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:

editText.requestFocus()
WindowCompat.getInsetsController(window, editText)!!.show(WindowInsetsCompat.Type.ime())

See also:
https://developer.android.com/develop/ui/views/touch-and-input/keyboard-input/visibility#ShowReliably

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文