AIR / as3 stage keylistener覆盖输入文本字段

时间:2013-03-21 14:01:18

标签: android ios actionscript-3 air keylistener

我正在使用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;

但是当我将焦点设置在文本字段时,软键盘会出现,但我无法输入单个字符。当我禁用keylistener时:没问题。

似乎监听器覆盖了我的输入字段。有没有解决方法呢?

1 个答案:

答案 0 :(得分:3)

我还为我的移动应用程序实现了后退按钮功能,但我曾经只在我的特定视图被激活时注册了keydown事件,并在视图被取消激活时删除了注册。

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();
}

这可以帮到你。