第一种方式:
直接修改Cocos2dxGLSurfaceView类中的onKeyDown方法,修改如下:
<span style="font-size:18px;">@Override public boolean onKeyDown(final int pKeyCode,final KeyEvent pKeyEvent) { switch (pKeyCode) { case KeyEvent.KEYCODE_BACK: Log.d("","KEYCODE_BACK+++++++++++++++++"); new AlertDialog.Builder(Spartacus.getActivity()) .setTitle(R.string.exit_tip) .setMessage(R.string.exit_message) .setNegativeButton(R.string.exit_cancel,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { } }) .setPositiveButton(R.string.exit_confirm,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int whichButton) { //finish(); System.exit(0); } }).show(); return true; 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); } }</span>
第二种方式:
1)、先修改修改Cocos2dxGLSurfaceView类中的onKeyDown方法,修改如下:
@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); } }
2)、在继承自Cocos2dxActivity类的,我们自己的activity类中覆盖父类的onKeyDown方法:
@Override public boolean onKeyDown(int keyCode,KeyEvent event) { //Log.d("","onKeyDown++++++++++++++++++"); if (keyCode == KeyEvent.KEYCODE_BACK) { new AlertDialog.Builder(this) .setTitle(R.string.exit_tip) .setMessage(R.string.exit_message) .setNegativeButton(R.string.exit_cancel,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { } }) .setPositiveButton(R.string.exit_confirm,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int whichButton) { finish(); System.exit(0); } }).show(); return true; } else { return super.onKeyDown(keyCode,event); } }
http://www.jb51.cc/article/p-pfzezpoz-vm.html
原文链接:https://www.f2er.com/cocos2dx/343987.html