Taking Control of the Flex Soft Keyboard
Join the DZone community and get the full member experience.
Join For Freethis is part 2 of my flex mobile series. please see my first post for information on getting started.
when using the text components, the android soft keyboard will automatically trigger upon focus as you would expect. however, sometimes you need finer grained control over when the soft keyboard gets triggered and what happens when it gets activated.
the soft keyboard in flex is controlled by the application focus. when a component that has the needssoftkeyboard property set to true is given the focus, the soft keyboard will come to the front and the stage will scroll so that the selected component is visible. when that component loses focus, the soft keyboard will disappear and the stage will return to its normal position.
with the understanding of the focus, you can control the soft keyboard by doing the following:
- to show the soft keyboard declaratively – set needssoftkeyboard to true for your component
- to show the soft keyboard programmatically – call requestsoftkeyboard() on a component that already has needssoftkeyboard set.
- to hide the soft keyboard – call setfocus() on a component that does not have needssoftkeyboard set.
this works fine for components that do not normally trigger the soft keyboard; however, for components that automatically raise the keyboard, setting needssoftkeyboard to false has no effect. a workaround to prevent the keyboard from popping up on these components is to listen for the activating event and suppressing it with code like the following:
<fx:script> <![cdata[ private function preventactivate(event:softkeyboardevent):void { event.preventdefault(); } ]]> </fx:script> <s:textarea text="i am a text component, but have no keyboard?" softkeyboardactivating="preventactivate(event)"/>
this code catches the softkeyboardactivating event on the textarea component and suppresses the default action of raising the soft keyboard.
in addition to getting events on activation, you can also catch softkeyboardactivate and softkeyboarddeactivate events in order to perform actions based on the soft keyboard status.
the following is the full code listing for a soft keyboard sample application that demonstrates all these techniques used together to take complete control over the soft keyboard.
this code creates several controls and attaches actions to them so that you can hide and show the soft keyboard at will, as well as see the current soft keyboard state as reported by the events that trickle up. when you run the application it should look like this:
notice that the textarea control, which normally triggers the soft keyboard no longer brings it up, while the highlighted button immediately raises the soft keyboard whenever it gets focus. the two buttons at the bottom to show and hide the keyboard merely play focus tricks to get flash to show and hide the keyboard at will.
you can use the same techniques in your own application to take full control over the soft keyboard.
from http://flash.steveonjava.com/taking-control-of-the-flex-soft-keyboard/
Opinions expressed by DZone contributors are their own.
Comments