您的位置:首页 > 移动开发 > Android开发

Air for Android开发中使用Back键弹出是否退出对话框

2014-03-04 22:02 603 查看
用Flash cs5做测试的时候试了很久都没有找到方法,按照官方的说明,“请注意:当按下“返回”键时,可调用事件 preventDefault() 方法以避免出现返回至上一应用程序的“返回”键默认行为。“菜单”和“搜索”键无默认行为。”,试了一遍又一遍,最终发现在使用event.preventDefault()方法时,如果侦听的是KeyUp事件的话,是无法阻止默认行为的,所以想要成功阻止返回,请侦听KeyDown事件

以下是一个用于测试的文档类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

//code
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;
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: