您的位置:首页 > 移动开发 > Cocos引擎

activity继承Cocos2dxActivity后无法监听安卓返回按键

2015-02-27 19:46 351 查看
前提:

public class TestActivity  extends Cocos2dxActivity {
@Override
public boolean onKeyDown( int keyCode, KeyEvent event) {// 监听不到


原因:

Called when a key was pressed down and not handled by any of the views inside of the activity. So, for example, key presses while the cursor is inside a TextView will not trigger the event (unless it is a navigation to another
object) because TextView handles its own key presses.If the focused view didn't want this event, this method is called.

处理:
    修改org.cocos2dx.lib.Cocos2dxGLSurfaceView
        原:
@Override
public boolean onKeyDown (final int pKeyCode, final KeyEvent pKeyEvent) {
switch (pKeyCode) {
case KeyEvent. KEYCODE_BACK:
case KeyEvent. KEYCODE_MENU:
this.queueEvent( new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.
mCocos2dxRenderer.handleKeyDown(pKeyCode);
}
});
return true;
default:
return super.onKeyDown(pKeyCode, pKeyEvent);
}
}
     改:
@Override
public boolean onKeyDown( final int pKeyCode, final KeyEvent pKeyEvent) {
switch (pKeyCode) {
case KeyEvent. KEYCODE_BACK:
return false;
case KeyEvent. KEYCODE_MENU:
this.queueEvent( new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.
mCocos2dxRenderer.handleKeyDown(pKeyCode);
}
});
return true;
default:
return super.onKeyDown(pKeyCode, pKeyEvent);
}
}
经测试可正常监听返回按键

参考:http://50vip.com/blog.php?i=390
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息