简体   繁体   中英

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 .

To test it on AVD you need to disable the keyboard.

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.

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. Moreover, calling requestFocus() does not give your View focus unless you first call setFocusableInTouchMode() and/or setFocusable() to true.

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

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