gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:07:39 25 4
gpt4 key购买 nike

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

  • 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);
}

处理程序收到消息,但我没有软键盘。为了捕捉文本,我写了一些类:

public class TextInputWatcher implements TextWatcher

和:

textEdit.addTextChangedListener(/*TextInputWatcher instance*/);

或者扩展 TextEdit 以便它在后面捕获输入的文本或回车键。

P.S. 我有一个平板电脑 - 变形金刚,所以附有一个硬件键盘。我试过有没有,但没有区别。那么额外的问题 - 如果有硬件键盘,它会阻止弹出软件键盘吗?然后如何从中获取输入?

最佳答案

显示键盘:

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

隐藏键盘:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

关于java - 使用 GLSurfaceView 显示软件键盘(并从中获取输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10432763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com