首页 文章

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

提问于
浏览
15

我正在Android中创建一个简单的打字游戏 . 我从物理键盘输入输入没有问题,但现在我试图让软键盘在没有EditText的情况下出现 . 到目前为止,我尝试过以下方法:

1. 具有visibility = "invisible"的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?

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

5 回答

  • 2

    以下代码适用于我:

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

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

  • 5

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

  • 2

    您可以使用以下命令强制显示软键盘:

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

    确保为视图启用软键盘:

    setFocusable(true);
    setFocusableInTouchMode(true);
    

    然后打电话:

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

    请注意,如果您在横向模式下工作,则软输入将创建自己的文本输入字段,从而破坏您的所有辛勤工作 . 您可以阻止此行为:

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

    现在,如果您设置了一个不可见的EditText,它将保持原样 .

相关问题