Show soft keyboard for dialog

≡放荡痞女 提交于 2019-12-02 16:04:40

Awesome question, I was trying to do that too and found a solution.

Using the dialog builder class AlertDialog.Builder you will have to invoke the dialog like this:

AlertDialog.Builder builder = new AlertDialog.Builder();
AlertDialog dialog;

builder.set...

dialog = builder.create();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();

This worked fine for me.

Note: you must import android.view.WindowManager.LayoutParams; for the constant value there.

 AlertDialog dialog = new AlertDialog.Builder(this).create();
    dialog.show();
    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);

It seems to be impossible.

So I made a new Activity instead of the Dialog and let the user edit there. Note, that in activities you can set the keyboard mode in the manifest file. I set it to show, when the activity opens.

Also note, that testing on the emulator with hard keys will not open the keyboard for SHOW_IMPLICIT or 0 flag.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!