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

前端 未结 12 1209
一生所求
一生所求 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:40

    I've managed to solve it like this:

    Dialog = builder.create();
    Dialog.show();
    Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    Dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
    0 讨论(0)
  • 2020-11-30 23:40

    A much better solution is given here.

    dialog.getWindow().clearFlags(
             WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            |WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    

    No workaround. EditText behaves as expected.

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

    In my case, the SoftInputMode wasn't getting displayed when I set it which was before showing the dialog (after creating it). The below code worked for me where I set the SoftInputMode after showing the dialog.

    Kotlin:

    val dialog = MaterialAlertDialogBuilder(context) // Other builder code
                    .create()
    dialog.show()
    dialog.window?.apply { // After the window is created, get the SoftInputMode
        clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
    }
    

    Java:

    AlertDialog dialog = MaterialAlertDialogBuilder(getContext()) // Other builder code
                    .create();
    dialog.show();
    Window window = dialog.getWindow();
    if(window != null){ // After the window is created, get the SoftInputMode
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
    

    I hope this helps anyone who was having the same problem as me.

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

    In my case the only way I was able to show the keyboard when the Dialog was shown was by adding to my DialogFragment:

    @Override
    public void onResume() {
        super.onResume();
        getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        myEditText.requestFocus();
    }
    

    Note the SOFT_INPUT_STATE_ALWAYS_VISIBLE instead of SOFT_INPUT_STATE_VISIBLE.

    From documentation:

    // Visibility state for softInputMode: please always make the soft input 
    // area visible when this window receives input focus.
    int SOFT_INPUT_STATE_ALWAYS_VISIBLE;
    
    0 讨论(0)
  • 2020-11-30 23:48

    When you call showDialog() to show a Dialog created using AlertDialog in onCreateDialog()

    You should put the code in onPrepareDialog():

    @Override
    protected void onPrepareDialog (int id, Dialog dialog, Bundle args)
    {
        TextView editText=(TextView) dialog.findViewById(R....);
    
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
           @Override
           public void onFocusChange(View v, boolean hasFocus) {
             if (hasFocus) {
                dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
             }
           }
        });
    }
    
    0 讨论(0)
  • 2020-11-30 23:52
    final AlertDialog.Builder alert = new AlertDialog.Builder(context);
    
    final AlertDialog dialog = alert.show();
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
    0 讨论(0)
提交回复
热议问题