Android show softkeyboard with showSoftInput is not working?

后端 未结 12 2162
抹茶落季
抹茶落季 2020-12-28 13:49

I have created a trivial application to test the following functionality. When my activity launches, it needs to be launched with the softkeyboard open.

My code doe

12条回答
  •  臣服心动
    2020-12-28 14:16

    When the activity launches It seems that the keyboard is initially displayed but hidden by something else, because the following works (but is actually a dirty work-around):

    First Method

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    editText.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            editText.requestFocus();
            imm.showSoftInput(editText, 0);
        }
    }, 100);
    

    Second method

    in onCreate to launch it on activity create

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() 
        {
        //  InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        //    inputMethodManager.toggleSoftInputFromWindow(EnterYourViewHere.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
    
            if (inputMethodManager != null)
            {
                inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
            } 
        }
    }, 200);
    

    Third Method ADD given code to activity tag in Manifest. It will show keyboard on launch, and set the first focus to your desire view.

    android:windowSoftInputMode="stateVisible"
    

提交回复
热议问题