when using AlertDialog.Builder with EditText, the Soft Keyboard doesn't pop

前端 未结 12 1333
一生所求
一生所求 2020-11-30 23:13

I am using AlertDialog.Builder in order to create an input box, with EditText as the input method.

Unfortunately, the Soft Keyboard doesn\'t pop, al

12条回答
  •  甜味超标
    2020-11-30 23:52

    This problem occurs when EditText is added after AlertDialog.onCreate is called.

    https://developer.android.com/reference/androidx/appcompat/app/AlertDialog.Builder

    The AlertDialog class takes care of automatically setting android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM for you based on whether any views in the dialog return true from View.onCheckIsTextEditor().

    You need to clear the FLAG_ALT_FOCUSABLE_IM flag.

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 
    

    Because AlertDialog.show is called in the DialogFragment.onStart, you can insert the code in the DialogFragment.onStart.

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }
    

    Or you can use the Dialog.setOnShowListener if you do not use a DialogFragment.

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        }
    });
    

提交回复
热议问题