Hide to show and hide keyboard in DialogFragment

后端 未结 8 2194
花落未央
花落未央 2021-02-08 03:57

In my dialog fragment, I am able to show the keyboard using

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT STATE_VISIBLE);
         


        
8条回答
  •  灰色年华
    2021-02-08 04:59

    The solution turned out to a combination of the following. To show the keyboard in a DialogFragment:

        @Override
        public void onResume()
        {
            super.onResume();
            editText.post(new Runnable()
            {
                @Override
                public void run()
                {
                    editText.requestFocus();
                    InputMethodManager imm =
                        (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null)
                        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    

    To hide it, use the solution above by @Shekhar

        @Override
        public void onDismiss(DialogInterface dialog)
        {
            InputMethodManager imm =
                (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm.isActive())
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    
            super.onDismiss(dialog);
        }
    

提交回复
热议问题