cocos2dx android返回键- 弹出退出对话框

前端之家收集整理的这篇文章主要介绍了cocos2dx android返回键- 弹出退出对话框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

第一种方式:

直接修改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

http://www.jb51.cc/article/p-vgerwjvc-bkc.html

http://50vip.com/390.html

原文链接:https://www.f2er.com/cocos2dx/343987.html

猜你在找的Cocos2d-x相关文章