简体   繁体   中英

Show Soft Keyboard when EditText Appears

I have this code in my android app, for an alert dialog and keyboard, and when the dialog is shown I want the keyboard to appear. But its not and I am not sure why?

Here is my code:

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

    alert.setTitle("Title");
    alert.setMessage("Message");

    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    alert.setView(input);

    input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                Log.i(TAG, "in focus");
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
            }
        }
    });

But the keyboard does not appear even though the edit text is in focus, and I tell it to appear why, doesn't it come up?

Thanks for the help in advance.

I tested your code and the keyboard opened when alert.show() was executed. If you are using an emulator be sure that you have the virtual keyboard enabled. Otherwise a keyboard will never appear on the device screen.

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