Can I use the soft keyboard without an EditText?

泪湿孤枕 提交于 2019-11-30 08:26:05

The following code works for me:

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

I use this code in a onKeyUp handler when I press the "menu" button.

Instead of using visibility="invisible" you can set android:alpha="0" on your EditText. So you still need a EditText but it is not visible and you can get the input from the softkeyboard by an onKeyListener()

You can force the Softkeyboard to be shown by using:


InputMethodManager im = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(myView, InputMethodManager.SHOW_FORCED);

Make sure to enable the soft keyboard for your view:

setFocusable(true);
setFocusableInTouchMode(true);

Then call:

InputMethodManager mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);

Note that if you are working in landscape mode, the soft input will create its own text input field ruining all your hard work. You can prevent this behavior:

// This makes us remain invisible when in landscape mode.
setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);

Now if you have set up an invisible EditText it will remain as you made it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!