官术网_书友最值得收藏!

Responding to Android soft-key interactions

AIR for Android does not include support for invoking the native operating system options menu that often appears at the bottom of the screen. However, there are ways of simulating the native behaviour, which we will explore in this section.

The normal behaviour of the back button, on Android, is to step back through the application states until we arrive back home. A further press of the back button will exit the application. By default, AIR for Android applications behave in this way as well. If we want to override this default behaviour, we must set up a mechanism to intercept this interaction and then prevent it.

How to do it...

We can respond to soft-key events through standard ActionScript event listeners.

  1. First, import the following classes into your project:
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.KeyboardEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.ui.Keyboard;
    
  2. Declare a TextField and TextFormat object to allow visible output upon the device:
    private var traceField:TextField;
    private var traceFormat:TextFormat;
    
  3. We will then set up our TextField, apply a TextFormat, and add it to the DisplayList. Here, we create a method to perform all of these actions for us:
    protected function setupTextField():void {
    traceFormat = new TextFormat();
    traceFormat.bold = true;
    traceFormat.font = "_sans";
    traceFormat.size = 32;
    traceFormat.align = "center";
    traceFormat.color = 0x333333;
    traceField = new TextField();
    traceField.defaultTextFormat = traceFormat;
    traceField.selectable = false;
    traceField.mouseEnabled = false;
    traceField.width = stage.stageWidth;
    traceField.height = stage.stageHeight;
    addChild(traceField);
    }
    
  4. Now we need to set an event listener on the Stage to respond to keyboard presses:
    protected function registerListeners():void {
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
    }
    
  5. We will then write a switch/case statement in our keyDown method that will perform different actions in response to specific soft-key events. In this case, we output the name of a specific menu item to our TextField:
    protected function keyDown(e:KeyboardEvent):void {
    var key:uint = e.keyCode;
    traceField.text = key + " pressed!\n";
    switch(key){
    case Keyboard.BACK:{
    e.preventDefault();
    traceField.appendText("Keyboard.BACK");
    break;
    }
    case Keyboard.MENU:{
    traceField.appendText("Keyboard.MENU");
    break;
    }
    case Keyboard.SEARCH:{
    traceField.appendText("Keyboard.SEARCH");
    break;
    }
    }
    }
    
  6. The result will appear similar to the following:

How it works...

We register listeners for these Android device soft-keys just as we would for a physical or virtual keyboard in ActionScript. If developing Android applications using AIR for Android, we also have access to the BACK, MENU, and SEARCH constants through the Keyboard class.

Registering a keyboard keyDown listener and then responding to specific key values through a switch/case statement allows us to respond to the interaction appropriately. For instance, if the MENU soft-key interaction is detected, we can reveal an options menu.

There's more...

There is also a HOME soft-key on Android devices. This key press cannot be captured through ActionScript as it exists solely to return the user to the Android home screen from any opened application.

Note

We must use the keyDown event when we want to cancel the default Android behavior of the BACK key because the keyUp event will fire too late and not be caught at all.

主站蜘蛛池模板: 化德县| 蓝田县| 新竹县| 砚山县| 赣榆县| 巧家县| 邵武市| 舞阳县| 鄄城县| 楚雄市| 晋中市| 盘锦市| 阳原县| 布尔津县| 成安县| 长岛县| 大余县| 黑河市| 邢台县| 三原县| 万山特区| 濮阳市| 丰县| 板桥市| 永昌县| 江安县| 阆中市| 永川市| 同心县| 泸西县| 偃师市| 武夷山市| 余姚市| 庄浪县| 铅山县| 扎鲁特旗| 商洛市| 治县。| 江陵县| 青冈县| 屯昌县|