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

前提:

    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





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值