Android Show Soft Keyboard When First Activity Starts?

前端 未结 3 1879
遥遥无期
遥遥无期 2020-12-29 16:47

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

相关标签:
3条回答
  • 2020-12-29 16:57

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

    0 讨论(0)
  • 2020-12-29 17:06

    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.

    0 讨论(0)
  • 2020-12-29 17:10

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题