我正在使用 Adobe Flash Builder 4.6 构建移动 AIR 应用程序(Android 和 IOS),但遇到了这个恼人的问题。
因为我想“捕捉”Android 设备上的后退键,所以我将以下代码添加到我的主类中:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
private function keyDown(k:KeyboardEvent):void {
if(k.keyCode == Keyboard.BACK) {
backClicked(); // function handling the back-action, not important
k.preventDefault();
}
现在在其他地方 - 嵌套在某些类中 - 我有一个文本字段:
TF = new TextField();
TF.type = TextFieldType.INPUT;
但是当我将焦点放在文本字段上时,软键盘确实出现,但我无法输入单个字符。当我禁用按键监听器时:没问题。
似乎监听器正在覆盖我的输入字段。有什么解决办法吗?
最佳答案
我还为我的移动应用程序实现了后退按钮功能,但我过去仅在激活我的特定 View 时才注册 keydown 事件,并在 View 停用时删除注册的事件。
in <s:view ....... viewActivate ="enableHardwareKeyListeners(event)" viewDeactivate="destroyHardwareKeyListeners(event)">
// add listener only for android device
if (Check for android device) {
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleHardwareKeysDown, false, 0);
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_UP, handleHardwareKeysUp, false, 0);
this.setFocus();
}
private function destroyHardwareKeyListeners(event:ViewNavigatorEvent):void
{
if (NativeApplication.nativeApplication.hasEventListener(KeyboardEvent.KEY_DOWN))
NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_DOWN, handleHardwareKeysDown);
if (NativeApplication.nativeApplication.hasEventListener(KeyboardEvent.KEY_UP))
NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_UP, handleHardwareKeysUp);
}
private function handleHardwareKeysDown(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.BACK) {
e.preventDefault();
// your code
} else {
}
}
private function handleHardwareKeysUp(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.BACK)
e.preventDefault();
}
愿这可以帮助你。
关于android - AIR/as3 阶段按键监听器覆盖输入文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15549669/