Setting the focus color on a Spark TextInput control in Flex 4

The following example shows how you can set the focus color on a Spark TextInput control in Flex 4 by setting the focusColor style.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/05/03/setting-the-focus-color-on-a-spark-textinput-control-in-flex-4/ -->
<s:Application name="Spark_TextInput_focusColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="focusColor:">
                <mx:ColorPicker id="clrPckr" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <s:TextInput id="txtInp"
            focusColor="{clrPckr.selectedColor}"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

[GoogleAdsWide]

View source is enabled in the following example.

You can also set the focusColor style in an external .CSS file or <Style/> block, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/05/03/setting-the-focus-color-on-a-spark-textinput-control-in-flex-4/ -->
<s:Application name="Spark_TextInput_focusColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        s|TextInput {
            focusColor: haloOrange;
        }
    </fx:Style>
 
    <s:TextInput id="txtInp"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

Or you can set the focusColor style using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/05/03/setting-the-focus-color-on-a-spark-textinput-control-in-flex-4/ -->
<s:Application name="Spark_TextInput_focusColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="focusColor:">
                <mx:ColorPicker id="clrPckr"
                        change="clrPckr_changeHandler(event);"/>
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
 
            protected function clrPckr_changeHandler(evt:ColorPickerEvent):void {
                txtInp.setStyle("focusColor", evt.color);
                callLater(txtInp.setFocus);
            }
        ]]>
    </fx:Script>
 
    <s:TextInput id="txtInp"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

7 thoughts on “Setting the focus color on a Spark TextInput control in Flex 4

  1. I noticed that you still have a Flex SDK beta version notice on the site. You might want to remove that now?

    “This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.”

  2. hi i’m mongolia
    i have one big problem can u help me

    i can’t write mongolia some characters “Ө” “Ү”
    this 2 character input “?????” write this
    why ??
    can tell me this problem plss!!!!

  3. hey guyss flash player 10
    i can’t write mongolia some characters “Ө” “Ү” for flash player 10
    this 2 character input “?????” write this
    why ??
    can tell me this problem plss!!!!

  4. Hi Peter, great Block! Is always a big help. Using it frquently since months.

    For this post: is there a way to completely get rid of the focusColor. I don’t want any glow around the textinput, when it is focused.

    Regards, Christian

    1. @Christian,

      Sure, you could set the focusSkin style to null, as seen in the following example:

       
      <?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" 
                     xmlns:mx="library://ns.adobe.com/flex/mx">
          <s:layout>
              <s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
          </s:layout>
       
          <fx:Style>
              @namespace s "library://ns.adobe.com/flex/spark";
       
              #ti2 {
                  focusSkin: ClassReference(null);
              }
          </fx:Style>
       
          <s:TextInput id="ti1" text="off (MXML)" focusSkin="{null}" />
          <s:TextInput id="ti2" text="off (CSS)" />
          <s:TextInput id="ti3" text="off (ActionScript)" initialize="ti3.setStyle('focusSkin', null);" />
       
          <s:TextInput id="ti4" text="on (default)" />
       
      </s:Application>

      Peter

      1. I’ve found that on “textArea” and other components,
        “focusSkin: ClassReference(null);” doesn’t remove the focus glow from showing.

        The only method I’ve found to remove focus from all elements is the css:

        * { focus-alpha: 0; }

  5. actually i am not able to use editable in flex 4.
    so please can any one tell me how can i use this

Comments are closed.