Can I use the soft keyboard without an EditText?

前端 未结 5 1369
傲寒
傲寒 2020-12-30 02:29

I\'m creating a simple typing game in Android. I have no problem getting input from the physical keyboard, but now I\'m trying to get the soft keyboard to appear without the

相关标签:
5条回答
  • 2020-12-30 02:56

    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()

    0 讨论(0)
  • 2020-12-30 03:03

    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.

    0 讨论(0)
  • 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);
    
    
    0 讨论(0)
  • 2020-12-30 03:04

    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);
    
    0 讨论(0)
  • 2020-12-30 03:08

    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.

    0 讨论(0)
提交回复
热议问题