- Flash Development for Android Cookbook
- Joseph Labrecque
- 712字
- 2025-03-31 04:57:15
Invoking the virtual keyboard programmatically
In most cases, simply giving focus to a text input field will invoke the virtual keyboard. Losing focus will dismiss the virtual keyboard. Perhaps we require our application to do this without user interaction, or immediately when entering a certain application state for convenience.
How to do it...
We configure a Shape
to toggle the Android virtual keyboard on and off through a Tap
touch event assigned to it.
- 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.SoftKeyboardEvent; import flash.events.TouchEvent; import flash.text.TextField; import flash.text.TextFormat; import flash.ui.Multitouch; import flash.ui.MultitouchInputMode;
- Declare a
Shape
alongside aTextField
andTextFormat
object. These will be used for interaction and visual feedback.private var tapBox:Sprite; private var tapBoxField:TextField; private var tapBoxFormat:TextFormat;
- Next, construct a method to handle the creation of our
Sprite
and add it to theDisplayList
. Tapping thisSprite
will allow us to invoke or hide the virtual keyboard. We will also construct aTextField
and associatedTextFormat
object within theSprite
to allow us to provide stateful messages to the user.protected function setupBox():void { tapBox = new Sprite(); tapBox.graphics.beginFill(0xFFFFFF, 1); tapBox.x = stage.stageWidth/2; tapBox.y = stage.stageHeight/2 - 200; tapBox.graphics.drawRect(-200,-100,400,160); tapBox.graphics.endFill(); tapBoxFormat = new TextFormat(); tapBoxFormat.bold = true; tapBoxFormat.font = "_sans"; tapBoxFormat.size = 42; tapBoxFormat.align = "center"; tapBoxFormat.color = 0x333333; tapBoxField = new TextField(); tapBoxField.defaultTextFormat = tapBoxFormat; tapBoxField.selectable = false; tapBoxField.mouseEnabled = false; tapBoxField.multiline = true; tapBoxField.wordWrap = true; tapBoxField.width = tapBox.width; tapBoxField.height = tapBox.height; tapBoxField.x = -200; tapBoxField.y = -80; tapBoxField.text = "Tap to Toggle Virtual Keyboard"; tapBox.addChild(tapBoxField); addChild(tapBox); }
- Set the specific input mode for the multitouch APIs to support touch input by setting
Multitouch.inputMode
to theMultitouchInputMode.TOUCH_POINT
constant and register an event listener on theDisplayObject
, which will be used to trigger the activation and deactivation of the Android virtual keyboard. In this case, aTouchEvent.TOUCH_TAP
event. A touch tap is the touch equivalent of a mouse click event. We can also register a number of listeners for a set of virtual keyboard events. In order for aDisplayObject
to be able to invoke the virtual keyboard, we will need to set itsneedsSoftKeyboard
property totrue
. TheSoftKeyboardEvent
listeners we register here are optional.protected function setupTouchEvents():void { Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; tapBox.needsSoftKeyboard = true; tapBox.addEventListener(TouchEvent.TOUCH_TAP, touchTap); tapBox.addEventListener (SoftKeyboardEvent. SOFT_KEYBOARD_ACTIVATING, vkActivating); tapBox.addEventListener(SoftKeyboardEvent. SOFT_KEYBOARD_ACTIVATE, vkActivate); tapBox.addEventListener(SoftKeyboardEvent. SOFT_KEYBOARD_DEACTIVATE, vkDeactivate); }
- To make use of the
SoftKeyboardEvent
listeners defined in the preceding point, we must create a variety of methods to execute once each activity is detected. In this way, we can monitor, interact with, or even prevent certain events from firing by intercepting the virtual keyboard while in the midst of activating, or detecting when the virtual keyboard has completed activation or deactivation completely.protected function vkActivating(e:SoftKeyboardEvent):void { trace("Virtual Keyboard ACTIVATING"); } protected function vkActivate(e:SoftKeyboardEvent):void { trace("Virtual Keyboard ACTIVATED"); } protected function vkDeactivate(e:SoftKeyboardEvent):void { trace("Virtual Keyboard DEACTIVATED"); }
- To invoke the virtual keyboard, we simply invoke
requestSoftKeyboard()on
theDisplayObject
, whoseneedsSoftKeyboard
property has been set totrue
. Here, we are checking to see whetherneedsSoftKeyboard
is set to true or not, and toggling this property based upon that.protected function touchTap(e:TouchEvent):void { if(tapBox.needsSoftKeyboard == true){ tapBox.requestSoftKeyboard(); tapBoxField.text = "Virtual Keyboard is Up"; tapBox.needsSoftKeyboard = false; }else{ tapBox.needsSoftKeyboard = true; tapBoxField.text = "Virtual Keyboard is Down"; } }
- To dismiss the virtual keyboard, the user will need to tap upon a
DisplayObject
, whoseneedsSoftKeyboard
property has been set tofalse
. - The result will appear similar to the following:
How it works...
In order to invoke the Android virtual keyboard through ActionScript, we must set an interactive DisplayObjects.needsSoftKeyboard
property to true
. This will allow us to register a tap touch listener and invoke requestSoftKeyboard()
upon the tap touch event being fired, revealing the virtual keyboard on screen.
Touching any DisplayObject
whose needsSoftKeyboard
property is set to false
(the default state), will dismiss the virtual keyboard. In our preceding example, we switch this property from true
to false
in order to make the DisplayObject
function as a toggle control.