简体   繁体   English

第一个活动开始时Android显示软键盘?

[英]Android Show Soft Keyboard When First Activity Starts?

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 我在方法“OnCreate”中使用此代码来显示虚拟键盘

    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. 我试图把这个代码放在事件“OnCreate”等等......但它似乎不起作用。

is there anyway to "force" to display the keyboard when I start the application? 无论如何,当我启动应用程序时,“强制”显示键盘?

Thanks in advance. 提前致谢。

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 !!! 谢谢 !!!

onCreate will not be called if the activity is first brought from background. 如果活动首先来自后台,则不会调用onCreate。 Have you tried put that code in onResume? 您是否尝试过将该代码放入onResume?

onCreate is called only when activity is first start or the activity is killed and user navigate to the activity again. 只有在活动首次启动或活动被终止且用户再次导航到活动时才会调用onCreate。 So if the activity is still alive but in background, it will not call onCreate. 因此,如果活动仍处于活动状态但在后台,则不会调用onCreate。

On the other hand, onResume will be called every time the activity comes to foreground (visible on screen) from background. 另一方面,每次活动从背景到达前景(在屏幕上可见)时,都会调用onResume。

Here is link to activity life cycle if you are interested http://developer.android.com/reference/android/app/Activity.html . 如果您对http://developer.android.com/reference/android/app/Activity.html感兴趣,可以在此链接到活动生命周期。

Hope it helps. 希望能帮助到你。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM