如何在 PopupWindow 上显示键盘?

4

我正在使用PopupWindow类,PopupWindow上有一个EditText,我的问题是当PopupWindow可见时,我点击EditText时软键盘不可见,因此我无法输入。请问有人能告诉我如何解决这个问题吗?

4个回答

15

创建新的PopupWindow时,使用另一个构造方法,你必须设置focusable = true;只有视图可聚焦时,软键盘才会显示。

public PopupWindow(View contentView, int width, int height, boolean focusable) {}

默认focusable为'false'


谢谢,我错过了那个构造函数。 - codeMagic

5
花费了一些时间才弄明白,但是现在我给你解释一下:
在创建弹出窗口时,我必须将文本框(Edittext)设置为在获得焦点时强制打开软键盘。
 txtBox.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus == true){
                InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                inputMgr.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);

            }
        }
    });
    txtBox.requestFocus();

4

Add this code popupWindow.setFocusable(true);


2
这对我有用。
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(final View v, final boolean hasFocus) {
            if (hasFocus && editText.isEnabled() && editText.isFocusable()) {
                editText.post(new Runnable() {
                    @Override
                    public void run() {
                        final InputMethodManager imm =(InputMethodManager)getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT);
                    }
                });
            }
        }
    });

这在我的情况下起作用了。尝试使用popupWindow.update(),但对我来说没有起作用。 - esfox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接