Android show softkeyboard with showSoftInput is not working?

后端 未结 12 1990
抹茶落季
抹茶落季 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:14

    If you're trying to show the soft keyboard in a Fragment, you need to wait until the Activity has been created before calling showSoftInput(). Sample code:

    public class SampleFragment extends Fragment {
    
        private InputMethodManager mImm;
        private TextView mTextView;
    
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        }
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            showSoftKeyboard(mTextView);
        }
    
        /**
         * Request the InputMethodManager show the soft keyboard.  Call this in {@link #onActivityCreated(Bundle)}.
         * @param view the View which would like to receive text input from the soft keyboard
         */
        public void showSoftKeyboard(View view) {
            if (view.requestFocus()) {
                mImm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-28 14:15

    This works for me:

    public void requestFocusAndShowSoftInput(View view){
        view.requestFocus();
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
    
    0 讨论(0)
  • 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"
    
    0 讨论(0)
  • 2020-12-28 14:17

    Following worked for me:

        mEditTextStudy.requestFocus();
        mEditTextStudy.post(
                new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager imm =
                                (InputMethodManager)
                                        getActivity()
                                                .getSystemService(Context.INPUT_METHOD_SERVICE);
                        if (imm != null) {
                            imm.showSoftInput(mEditTextStudy, SHOW_FORCED);
                        }
                    }
                });
    
    0 讨论(0)
  • 2020-12-28 14:21

    This worked with me on a phone with hard keyboard:

    editText1.requestFocus();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    
    0 讨论(0)
  • 2020-12-28 14:23

    This answer maybe late but it works perfectly for me. Maybe it helps someone :)

    public void showSoftKeyboard(View view) {
        if (view.requestFocus()) {
            InputMethodManager imm = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            boolean isShowing = imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
            if (!isShowing)
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }
    }
    

    Depends on you need, you can use other flags

    InputMethodManager.SHOW_FORCED
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
    0 讨论(0)
提交回复
热议问题