0
 AlertDialog.Builder alert = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

            }
        });
        alert.show();

InputMethodManager imm = (InputMethodManager) getSystemService(MainActivity.INPUT_METHOD_SERVICE);
        imm.showSoftInput(input, InputMethodManager.SHOW_FORCED);

Я создаю АлертДиалогОкно с полем для ввода. Надо, чтобы сразу появлялась клавиатура, но она не появляется. Я понимаю, что ошибка в showSoftInput и там должен быть не input, а именно часть из уже созданного AlertDialog. Как ее получить?

1 ответ 1

4

Вам нужно для начала перевести фокус на нужное view, в нашем случае editText и далее вызвать для него клавиатуру. Вот такой метод можно использовать:

protected void showSoftInput(View view) {
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (view != null) {
            view.post(() -> {
                view.requestFocus();
                inputManager.showSoftInput(view, 0);
            });
        }
    }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.