3

我想为我的 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);
    }

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

public class TextInputWatcher implements TextWatcher

和:

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

或扩展 aTextEdit以便它在背面捕获输入的文本或输入键。

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

4

2 回答 2

5

显示键盘:

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

隐藏键盘:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
于 2012-05-03T13:56:24.180 回答
1

我做了2D游戏。我想你以前和我有同样的问题。试试这个:

class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {

private static DrawThread _thread;    

public DrawingPanel(Context context, AttributeSet attrs) {
    super(context, attrs);
    getHolder().addCallback(this);       
   _thread = new DrawThread(getHolder(), this);
}
....

布局“游戏视图”:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" 
android:layout_height="fill_parent">
<!-- YOUR SURFACE -->
<com.yourcompany.DrawingPanel android:id="@+id/surfaceView" android:layout_width="fill_parent" 
android:layout_height="fill_parent"></com.yourcompany.DrawingPanel>

<!-- YOUR BUTTONS -->
<RelativeLayout android:id="@+id/controlPanel" android:layout_width="fill_parent" android:orientation="horizontal"
android:layout_height="fill_parent" >   
<RelativeLayout android:layout_width="50px" android:orientation="vertical"
    android:layout_height="fill_parent"  android:gravity="center_vertical" android:layout_alignParentLeft="true">
         <Button android:id="@+id/leftButton" android:layout_width="wrap_content" 

        android:layout_height="50px" android:background="@xml/button_left_state"/> 
        <Button android:id="@+id/upgradeButton" android:layout_width="wrap_content" 
        android:layout_below="@id/leftButton"
        android:layout_height="50px" android:background="@xml/button_upgrade_state"/> 
</RelativeLayout>
</RelativeLayout>           
 </FrameLayout>

然后你应该在游戏活动中设置内容,如下所示:

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.gameview);
   ...

希望它可以帮助你。

于 2012-05-03T14:04:03.223 回答