我可以在没有EditText的情况下使用软键盘吗?

我正在Android中创建一个简单的打字游戏。 我从物理键盘获取输入没有问题,但现在我试图让软键盘在没有EditText的情况下出现。 到目前为止,我已经尝试了以下内容:

1.可见性=“不可见”的EditText和这一行:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(keyboard_edittext, InputMethodManager.SHOW_FORCED); // SHOW_IMPLICIT also failed

2. onCreate()中的这一行:

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

这种方法实际上在屏幕的底部10%显示了一个空的白色框,但不是键盘,尽管现在我运行它时什么都不做。

3. onCreate()中的另外两行:

InputMethodManager m = (InputMethodManager)this.getSystemService (Context.INPUT_METHOD_SERVICE); m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);

没有运气在任何这些。 是否可以显示软键盘(然后使用onKeyUp / onKeyDown )而不关注EditText?

现在,我能看到的唯一方法就是创建我自己的软键盘实现(即从头构建)。 不期待那!


以下代码适用于我:

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

当我按下“菜单”按钮时,我在onKeyUp处理程序中使用此代码。


您可以在EditText上设置android:alpha="0" ,而不是使用visibility="invisible" 。 所以你仍然需要一个EditText,但它是不可见的,你可以通过onKeyListener()来获得softkeyboard的输入。


您可以使用以下方式强制显示Softkeyboard:


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

链接地址: http://www.djcxy.com/p/93377.html

上一篇: Can I use the soft keyboard without an EditText?

下一篇: How can I set the focus (and display the keyboard) on my EditText programmatically