using setFocus() method to bring focus to a Textinput control.

To bring focus to a Textinput control we have to use some bit of AS.

And the method to be used is setFocus().

EX: textinputreference.setFocus();

This will bring the focus to the Textinput of ID  textinputreference.

The below example explains the need.

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”init()”>

<mx:TextInput id=”search” x=”10″ y=”71″/>

<mx:Script>
<![CDATA[
private function init():void
{
search.setFocus();
}
]]>
</mx:Script>

usable method.

Updated ::

Use the line addedToStage=”stage.focus=null;” in the Application tag or whereever you are using this setFocus(); methods.It sets the stage focus to null so that the current view has the focus.Little bit tricky.

It will defenetely work. :-)

9 Responses to “using setFocus() method to bring focus to a Textinput control.”

  1. wallville Says:

    This does not put the cursor into the field, it only sets the focus into the field. does anyone know how to load an application and place a cursor into a specific text field on creationComplete?

  2. joss Says:

    I’m having the same issue. the focus is ‘drawn around a text input filed but you cannot write into it. (I’m also havign a problem where you cannot click on teh field and hten write into it so there may be some other problem at work here.)

    however, i am also having trouble setting focus to teh first of a group of radio buttons or checkboxes.

    • kumargandhi Says:

      Hi,

      To resolve this problem we got to get the focus.Use this line addedToStage=”stage.focus=null;” , it will bring the focus to the current stage and sets the focus.Like if the Textfield is in TitleWindow so add this line to the TitleWindow.

      regards,
      kumar.

  3. Alexwebmaster Says:

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

  4. Steven Rieger Says:

    I’m still struggling with the focus issue.

    Trying all your suggestions still not working. Also using Firefox. Here is my code. . .

    Can you help?

  5. Aruna Says:

    Hi

    Please let me know the solution!
    I tried as u said, its not working :(

    Thanks
    Aruna

  6. Craig Says:

    Howzit all
    I came across this error as well
    after some searching found out that it is logged
    as a bug in Adobe but they have a work around:
    http://bugs.adobe.com/jira/browse/SDK-15319
    use the callLater() function
    with some tweaking i got this method to work for me

    I use it on a textInput field’s focusOut, if it fails a RegExp test, i set some vars, and on a focusIn method checks if the previous field failed, then the method sets focus back to the previous textInput as well as show an errorString. I made the focusIn and focusOut methods dynamic, so i use it over on all my required fields

    hopefully this helps

    here’s my code example:

    //mValidation is a Boolean
    //lPreviousItem is a String

    private function checkFocusInAmount(field:Event)
    :void {
    if(!mValidationError) {
    var lText:String = field.currentTarget.text;
    if(Number(lText) == 0) {
    field.currentTarget.text = “”;
    }
    }
    else {
    callLater(
    field.currentTarget.focusManager.setFocus
    [this[mPreviousItem]]);
    }
    }

    private function checkFocusOutAmount(field:Event)
    :void {
    var lText:String = field.currentTarget.text;
    var lTest:RegExp = /^[0-9]+([\.][0-9]{1,2})?$/;

    if(lText == “”) {
    field.currentTarget.text = “0.00″;
    }
    else {
    if(lTest.test(lText)) {
    field.currentTarget.text =
    Number(lText).toFixed(2);
    field.currentTarget.errorString = ”;
    mValidationError = false;
    }
    else {
    field.currentTarget.errorString =
    ‘Amount is in the incorrect format.
    \nOnly an Amount with up to 2 decimal
    places allowed.\nAmount format: #.##’;

    mValidationError = true;
    mPreviousItem = field.currentTarget.id;
    }
    }
    }

  7. Craig Says:

    sorry my code doesn’t look very neat, should still be readable

Leave a Reply