Android:当焦点在 EditText 上时自动显示软键盘 [英] Android: show soft keyboard automatically when focus is on an EditText

查看:32
本文介绍了Android:当焦点在 EditText 上时自动显示软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AlertDialog 显示一个输入框.当我调用 AlertDialog.show() 时,对话框本身内的 EditText 会自动聚焦,但不会自动显示软键盘.

I'm showing an input box using AlertDialog. The EditText inside the dialog itself is automatically focused when I call AlertDialog.show(), but the soft keyboard is not automatically shown.

如何让软键盘在显示对话框时自动显示?(并且没有物理/硬件键盘).类似于我按下搜索按钮调用全局搜索时,软键盘会自动显示.

How do I make the soft keyboard automatically show when the dialog is shown? (and there is no physical/hardware keyboard). Similar to how when I press the Search button to invoke the global search, the soft keyboard is automatically shown.

推荐答案

您可以在 AlertDialog 上的 EditText 上创建焦点侦听器,然后获取 AlertDialogWindow.从那里你可以通过调用 setSoftInputMode 来显示软键盘.

You can create a focus listener on the EditText on the AlertDialog, then get the AlertDialog's Window. From there you can make the soft keyboard show by calling setSoftInputMode.

final AlertDialog dialog = ...;

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

这篇关于Android:当焦点在 EditText 上时自动显示软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆