简体   繁体   English

显示软输入键盘

[英]Show soft input keyboard

I'm trying to show the soft input keyboard for a view on the touch event. 我正在尝试为触摸事件的视图显示软输入键盘。 This line works: 这条线有效:

inputManager.toggleSoftInputFromWindow(getWindowToken(),0,0);

But this line doesn't work: 但这条线不起作用:

inputManager.showSoftInput(this,0);

Why is it so? 为什么会这样? What if I want to connect the soft input to the view? 如果我想将软输入连接到视图怎么办? Thanks. 谢谢。

I think you are testing on emulator. 我认为你在模拟器上测试。 not on real device? 不是真正的设备?

It will not open the keyboard on AVD but it will open on real device, which does not have Hard key board . 它不会在AVD上打开keyboard ,但它会在没有Hard key board真实设备上打开。

To test it on AVD you need to disable the keyboard. 要在AVD上测试它,您需要禁用键盘。

To disable keyboard use 禁用键盘使用

Click on AVD manager > open you targeted AVD > Edit > Hardware > New > Keyboard Support > OK > Make it "NO"

try this: 尝试这个:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

try this in onclick event. 在onclick事件中尝试这个。

InputMethodManager imm = 
            (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 
                InputMethodManager.HIDE_IMPLICIT_ONLY);

showSoftInput() won't work unless your View has focus. 除非您的View具有焦点,否则showSoftInput()将无法工作。 Moreover, calling requestFocus() does not give your View focus unless you first call setFocusableInTouchMode() and/or setFocusable() to true. 此外,除非首先将setFocusableInTouchMode()和/或setFocusable()调用为true,否则调用requestFocus()不会给出View焦点。

You need to request focus first and show the soft input as follows: 您需要先请求焦点并显示软输入,如下所示:

    mEditTextStudy.requestFocus();
    mEditTextStudy.post(
            new Runnable() {
                @Override
                public void run() {
                    InputMethodManager imm =
                            (InputMethodManager)
                                    getActivity()
                                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.showSoftInput(mEditTextStudy, SHOW_FORCED);
                    }
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM