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

时间:2012-05-03 13:48:09

标签: 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);
    }

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

public class TextInputWatcher implements TextWatcher

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

或者延伸TextEdit,以便在背面捕获输入的文本或输入密钥。

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

2 个答案:

答案 0 :(得分:5)

显示键盘:

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

隐藏键盘:

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

答案 1 :(得分:2)

我做了第二场比赛。我觉得你之前有同样的问题。试试这个:

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);
   ...

希望它可以帮助你。

相关问题