【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 sunarm_jk 于 2019-6-29 22:37 编辑
,按照官方的说明,“请注意:当按下“返回”键时,可调用事件 preventDefault() 方法以避免出现返回至上一应用程序的“返回”键默认行为。“菜单”和“搜索”键无默认行为。”,试了一遍又一遍,最终发现在使用event.preventDefault()方法时,如果侦听的是KeyUp事件的话,是无法阻止默认行为的,所以想要成功阻止返回,请侦听KeyDown事件。 以下是一个用于测试的文档类: package{ import flash.display.Sprite; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.ui.Keyboard; import flash.desktop.NativeApplication; import flash.text.TextField; import flash.events.Event; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; /** * 描述: 测试阻止Air For Android 默认BACK按键事件 * 作者: Frozensun * 创建日期: 2010-12-14 21:03 */ public class Main extends Sprite { private var _menuBar:Sprite; private var _txtLogger:TextField; public function Main() { fCreateLogger(); fCreateMenuBar(); output("试试按下menu和返回键吧!"); NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, eMenuBarHandler); } //创建logger显示文本 private function fCreateLogger():void { _txtLogger = new TextField(); _txtLogger.width = stage.stageWidth; _txtLogger.height = stage.stageHeight; _txtLogger.defaultTextFormat = new TextFormat(null, 20, 0xCCCCCC); _txtLogger.multiline = true; _txtLogger.selectable = false; _txtLogger.background = true; _txtLogger.backgroundColor = 0x333333; addChild(_txtLogger); } //创建菜单 private function fCreateMenuBar():void { var __W:Number = stage.stageWidth / 1.5; var __H:Number = __W / 2; _menuBar = new Sprite(); _menuBar.graphics.beginFill(0xCCCCCC, 0.75); _menuBar.graphics.drawRoundRect(-__W / 2, -__H / 2, __W, __H, __H / 4, __H / 4); _menuBar.graphics.beginFill(0x666666, 0.8); _menuBar.graphics.drawCircle(0, 0, __H / 2 / 1.5); _menuBar.graphics.endFill(); _menuBar.x = stage.stageWidth / 2; _menuBar.y = stage.stageHeight / 2; var __txtClose:TextField = new TextField(); __txtClose.autoSize = TextFieldAutoSize.CENTER; __txtClose.defaultTextFormat = new TextFormat(null, 60, 0xCCCCCC, true); __txtClose.selectable = false; __txtClose.text = "X"; __txtClose.x = -__txtClose.width / 2; __txtClose.y = -__txtClose.height / 2; _menuBar.addChild(__txtClose); } //键盘事件 private function eMenuBarHandler(e:KeyboardEvent):void { switch (e.keyCode) { case Keyboard.MENU : checkMenuBar(); break; case Keyboard.BACK : e.preventDefault(); checkMenuBar(); break; } output("keyCode:", e.keyCode, " isDefaultPrevented:", e.isDefaultPrevented()); } //检测menuBar的显隐 private function checkMenuBar():void { if (! this.contains(_menuBar)) { this.addChild(_menuBar); _menuBar.addEventListener(MouseEvent.CLICK, eExitAppHandler); } else { this.removeChild(_menuBar); _menuBar.removeEventListener(MouseEvent.CLICK, eExitAppHandler); } } //按下_menuBar时退出 private function eExitAppHandler(e:MouseEvent):void { NativeApplication.nativeApplication.exit(); } //显示调试文本 private function output(...rest):void { var __log:String = ""; for each (var i:* in rest) { __log += String(i) + " "; } __log += "/n"; _txtLogger.appendText(__log); _txtLogger.scrollV = _txtLogger.maxScrollV; } } }
|