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

微笑、不失礼 提交于 2019-12-17 10:20:10

问题


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, although the EditText is in focus, unless you explicitly touch it again.

Is there a way to force it to pop?

I've tried the following, after the (AlertDialog.Builder).show(); but for no avail.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);

Anyone can help?

Thanks!!


回答1:


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



回答2:


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



回答3:


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



回答4:


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:

int SOFT_INPUT_STATE_ALWAYS_VISIBLE
Visibility state for softInputMode: please always make the soft input area visible when this window receives input focus.



回答5:


When you call showDialog to show a Dialogue created using AlertDialog in onCreateDialog

You should put the code here

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

}



回答6:


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.




回答7:


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



回答8:


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




回答9:


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



回答10:


final AlertDialog.Builder alert = new AlertDialog.Builder(context);

final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);



回答11:


Can you imagine I started here on my journey to getting the answer, only to have come and give the accurate answer.

Firstly let me point out that InputMethodManager.showSoftInput does not use SHOW_FORCED flag. It only uses 0 and SHOW_IMPLICIT.

SOLUTION 1

Here we understqnd that Android has problems. Alot of times Android functions fail to function because of some incomplete initialization. Sounds crazy, but then I consider that android objects dont behave normal. Normal objects are completely initialized at construction, but android objects must go through a sersies of function calls: onCreate, onStart, onThis, onThat.

Android knows this, thus each View has a post and postDelayed function; which post instruction on the view´s message queue, which is bound to be handled.

Following is working example from my project....

src/jav/android/Util.java

package jav.android;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.view.*;
import android.view.inputmethod.*;


public class Util 
{   
    public static void showKeyboard(final Context ctx,final View view) 
    {
        Runnable r = new Runnable()
        {
            @Override public void run()
            {
                if(view.requestFocus()) 
                {
                 InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
                 imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
                }
            }
        };

        view.post(r);
    }

    // Note that we dont need to use View.post() here
    public static void hideKeyboard(Context ctx,View view) 
    {
     InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
}

SOLUTION 2

  1. Add to xml as EditText content. This will ensure that EditText has the focus at the get go. So you wont have to request it programatically and thus no need to use View.post(Runnable).

    <...Layout>

  2. Use InputMethodManager to show the keyboard.

    InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);

    SUMMARY

My above answers answered your specific question. However, you may notice that keyboard pops up initially, but upon Activity minimize and popup back, the EditText has focus but the keyboard is missing. Another problem I had was when I press back, I wanted the keyboard to go missing forever, until user presses text box again.



来源:https://stackoverflow.com/questions/3455235/when-using-alertdialog-builder-with-edittext-the-soft-keyboard-doesnt-pop

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