简体   繁体   中英

Show soft keyboard for dialog

I am displaying a dialog with an edittext view. However, the softkeyboard will open only if the user presses inside the editview. So I tried calling an InputMethodManager with the following code.

InputMethodManager imm =
 (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(dialogField,0);

The dialogField is the input field. However, when exactly am I supposed to do this? I tried it in the onStart() method of the dialog, but nothing happens. I also tried requesting the focus for the dialogField before, but that changes nothing.

I also tried this code

dialogField.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
    public void onFocusChange (View v, boolean hasFocus)
    {
        if (hasFocus)
        {
            Main.log("here");
            dialogInput.getWindow().setSoftInputMode(
              WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            /*
                InputMethodManager mgr =
                  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.showSoftInput(dialogField,0);
            */
        }
    }
});

in both versions. But no soft keyboard would like to appear. The Main.log is just a log, which shows me that the function is actually called. And yes, it is called.

I could get the keyboard with the SHOW_FORCED flag before the dialog opens. But then it will not close on exit. And I can only do that BEFORE I show the dialog. Inside any callbacks it does not work either.

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);

Kotlin

Here's the tested code.

val dialog = AlertDialog.Builder(requireContext()).apply {
    setTitle(…)
    setView(editText)
    setPositiveButton(…)
    setNegativeButton(…)
}
val window = dialog.show().window
window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)

Make sure you access the window property from show() method. Getting window from create() method was returning null for me, so the keyboard wasn't showing.

Import AlertDialog from androidx.appcompat.app.AlertDialog . Import WindowManager from android.view .

Dialog Fragment With Kotlin

override onStart Method

override fun onStart() {
    super.onStart()
    dialog.window?.
    setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
    )
}

if you want to close after dismiss then override dismiss method with below code

override fun onDismiss(dialog: DialogInterface?) {
val inputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM