Android show softkeyboard with showSoftInput is not working?

后端 未结 12 2110
抹茶落季
抹茶落季 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);
            }
        }
    
    }
    

提交回复
热议问题