博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2dx android返回键- 弹出退出对话框
阅读量:4216 次
发布时间:2019-05-26

本文共 2377 字,大约阅读时间需要 7 分钟。

第一种方式:

直接修改Cocos2dxGLSurfaceView类中的onKeyDown方法,修改如下:

@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);		}	}
第二种方式:

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://cvsmi.baihongyu.com/

你可能感兴趣的文章
SD卡驱动分析--基于高通平台
查看>>
SD Card 驱动流程分析
查看>>
Linux之debugfs介绍
查看>>
关于sd卡中一些概念的理解
查看>>
sd卡驱动分析之相关硬件操作和总结
查看>>
好的播文
查看>>
linux dd命令解析
查看>>
linux find命令详解
查看>>
S3C2440上touchscreen触摸屏驱动
查看>>
USB History Viewing
查看>>
怎样做可靠的分布式锁,Redlock 真的可行么?
查看>>
[图文] Seata AT 模式分布式事务源码分析
查看>>
pm 源码分析
查看>>
Sending the User to Another App
查看>>
kmsg_dump
查看>>
Getting a Result from an Activity
查看>>
Allowing Other Apps to Start Your Activity
查看>>
dev/mem
查看>>
pfn_valid 源码分析
查看>>
dev/kmem 和dev/mem的区别
查看>>