DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Taking Control of the Flex Soft Keyboard

Stephen Chin user avatar by
Stephen Chin
·
Aug. 21, 11 · Interview
Like (1)
Save
Tweet
Share
11.87K Views

Join the DZone community and get the full member experience.

Join For Free

this 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.

<?xml version="1.0" encoding="utf-8"?>
<s:application xmlns:fx="http://ns.adobe.com/mxml/2009"
         xmlns:s="library://ns.adobe.com/flex/spark"
         splashscreenimage="@embed('proandroidflash400.png')">
  <fx:script>
    <![cdata[
      [bindable]
      private var state:string;
 
      [bindable]
      private var type:string;
 
      private function handleactivating(event:softkeyboardevent):void {
        state = "activating...";
        type = event.triggertype;
      }
 
      private function handleactivate(event:softkeyboardevent):void {
        state = "active";
        type = event.triggertype;
      }
 
      private function handledeactivate(event:softkeyboardevent):void {
        state = "deactive";
        type = event.triggertype;
      }
 
      private function preventactivate(event:softkeyboardevent):void {
        event.preventdefault();
      }
    ]]>
  </fx:script>
  <s:vgroup left="20" top="20" right="20" gap="15"
        softkeyboardactivating="handleactivating(event)"
        softkeyboardactivate="handleactivate(event)"
        softkeyboarddeactivate="handledeactivate(event)">
    <s:hgroup>
      <s:label text="keyboard state: " fontweight="bold"/>
      <s:label text="{state}"/>
    </s:hgroup>
    <s:hgroup>
      <s:label text="trigger type: " fontweight="bold"/>
      <s:label text="{type}"/>
    </s:hgroup>
    <s:button id="needy" label="i need the keyboard" needssoftkeyboard="true" emphasized="true"/>
    <s:textarea text="i am a text component, but have no keyboard?"
          softkeyboardactivating="preventactivate(event)"/>
    <s:hgroup width="100%" gap="15">
      <s:button label="hide keyboard" click="{setfocus()}" width="50%"/>
      <s:button label="show keyboard" click="{needy.requestsoftkeyboard()}" width="50%"/>
    </s:hgroup>
  </s:vgroup>
</s:application>

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/

FLEX (protocol)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DZone's Article Submission Guidelines
  • 11 Observability Tools You Should Know
  • Unlocking the Power of Elasticsearch: A Comprehensive Guide to Complex Search Use Cases
  • Spring Boot vs Eclipse Micro Profile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: