使用GLSurfaceView显示软件键盘(并从中获取输入)

Ari*_*hys 2 java android opengl-es opengl-es-2.0

我想为我的OpenGL ES 2.0应用程序获取用户输入,但有两个问题:

  • 1)如何将软件键盘带到我的应用程序前面?
  • 2)我怎样才能从中获取输入?

我试着用这个:

//OpenGL ES 2.0 view class
public class OGLES2View extends GLSurfaceView 
{
    private static final int OGLES_VERSION = 2;
    private static Handler softKeyboardHandler;
    private final static int SHOW_IME_KEYBOARD = 0;
    private final static int HIDE_IME_KEYBOARD = 1;
    private static EditText textEdit;
    private static  InputMethodManager imm;

    private void setSoftKeyboardHandler()
    {
        softKeyboardHandler = new Handler()
        {
            public void handleMessage(Message msg)
            {
                switch(msg.what)
                {
                    case SHOW_IME_KEYBOARD:
                        textEdit.requestFocus();
                        imm.showSoftInput(textEdit,inputMethodManager.SHOW_IMPLICIT);//Nothing happens
                        Log.i("GLVIEW","SHOW KEYBOARD");
                        break;

                    case HIDE_IME_KEYBOARD:
                        imm.hideSoftInput(textEdit, 0);
                        Log.i("GLVIEW","HIDE KEYBOARD");
                        break;

                    default:
                        break;
                }
            }
        };
    }

    public OGLES2View(Context context) 
    {
        super(context);
        textEdit = new EditText(context);
        setEGLContextClientVersion(OGLES_VERSION);
        setRenderer(new OGLES2Renderer());
        imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        setSoftKeyboardHandler();
    }

    public static void showIMEKeyboard() 
    {
        softKeyboardHandler.sendEmptyMessage(SHOW_IME_KEYBOARD);
    }

    public static void hideIMEKeyboard()
    {
        softKeyboardHandler.sendEmptyMessage(HIDE_IME_KEYBOARD);
    }

    //In main activity class
    private GLSurfaceView ogles2SurfaceView = null;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //...
        ogles2SurfaceView = new OGLES2View(this);
        setContentView(ogles2SurfaceView);
    }
Run Code Online (Sandbox Code Playgroud)

处理程序获取消息,但我没有软件键盘.为了捕捉文字,我写了一些课:

public class TextInputWatcher implements TextWatcher
Run Code Online (Sandbox Code Playgroud)

和:

textEdit.addTextChangedListener(/*TextInputWatcher instance*/);
Run Code Online (Sandbox Code Playgroud)

或延伸一个,TextEdit以便在背面捕获输入的文本或输入密钥.

PS我有一个平板电脑 - 变压器,所以有一个硬件键盘连接.我尝试了它没有,但没有区别.所以奖金问题 - 如果有硬件键盘会阻止软键盘弹出以及如何从中获取输入呢?

Rag*_*aju 5

显示键盘:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Run Code Online (Sandbox Code Playgroud)

隐藏键盘:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)