2012-05-03 71 views
1

我想獲得用戶的用戶輸入我的OpenGL ES 2.0應用程序,但也有2個問題:
1)如何使軟鍵盤我的應用程序的前面?
2)如何捕捉它的輸入?

我嘗試使用此:顯示軟件鍵盤GLSurfaceView(並從中獲取輸入)

//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*/); 

或者說,它捕獲的回inputed文本或回車鍵擴展文本編輯。

P.S.我有平板電腦 - 變壓器,所以有一個硬件鍵盤連接,我嘗試與它沒有,但沒有區別。所以獎金的問題 - 如果有一個硬件鍵盤,它會阻止軟件鍵盤彈出以及如何從它輸入?

+0

我不知道它是怎麼回事。你是否已經完成了該項目,如果沒有,是否有預覽? –

回答

4

顯示鍵盤:

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

隱藏鍵盤:

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

我使用EditText的警報,現在一切正常,謝謝! – Aristarhys

2

我做的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); 
} 
.... 

佈局 'gameview':

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

希望它可以幫助你。

+0

你的例子給我一個關於使用texbox提示的問題,謝謝! – Aristarhys