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

前端 未结 12 1210
一生所求
一生所求 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);
        }
    });
    
    0 讨论(0)
  • 2020-11-30 23:55
    Window window = dialog.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
    0 讨论(0)
  • 2020-11-30 23:57

    I've made such a thing

    AlertDialog.Builder b = new AlertDialog.Builder(this);//....
    AlertDialog dialog = b.create();
    
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
    dialog.show();
    
    0 讨论(0)
  • 2020-11-30 23:59

    This was answered here already. Using an OnFocusChangeListener worked for me.

    0 讨论(0)
  • 2020-11-30 23:59

    Try this, its working for me

    If you want to display soft keyboard:

    InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(input.getWindowToken(), 0);
    

    And if you want to hide the it:

      InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
    
    0 讨论(0)
  • 2020-12-01 00:04

    I found out that the same code works properly on Tablet, the keyboard does pop up, but on Phone it doesn't, so researching further, seems to point to the "adjust" option.

    I am using this, feels much cleaner.

    AlertDialog d = builder.create();
    d.getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    d.show();
    
    0 讨论(0)
提交回复
热议问题