Nifty62
Nifty62

Reputation: 92

How to alter the active text field in AS3?

I have 6 text fields on the stage along with a keypad with the numbers 0-9.

Ultimately this is going to be an Android app. And I want to be able the select a text field and then press the keypad buttons and have the numbers appear in the active text field.

I've been trying to Google active field and similar searches and can't seem to find a reference.

Keep in mind I'm fumbling around in the dark from what I've tried to gather from multiple tutorials. This code is probably complete garbage:

package  {

public class main {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import fl.managers.FocusManager;
    var focus:FocusManager = new FocusManager(this);

    btn_1.addEventListener(MouseEvent.MOUSE_DOWN, inputNumber);

    public function inputNumber(m:MouseEvent){
            focus.text = 1;
    }

    public function main() {
        // constructor code
    }

}

}

Current Errors:

C:\Users\Otaku\Documents\lottocount\main.as, Line 19, Column 35 1118: Implicit coercion of a value with static type flash.display:InteractiveObject to a possibly unrelated type flash.text:TextField.

C:\Users\Otaku\Documents\lottocount\main.as, Line 14, Column 44 1120: Access of undefined property onMyButtonClick.

C:\Users\Otaku\Documents\lottocount\main.as, Line 14, Column 3  1120: Access of undefined property btn_1.

C:\Users\Otaku\Documents\lottocount\main.as, Line 13, Column 47 1120: Access of undefined property onFocusIn.

C:\Users\Otaku\Documents\lottocount\main.as, Line 13, Column 3  1120: Access of undefined property stage.

Upvotes: 0

Views: 294

Answers (1)

Creative Magic
Creative Magic

Reputation: 3141

If you access the focus object, you'll have a reference to your currently active (focused on) object.

But in reality, just having a TextField that allows input will open a keyboard view (only on mobile) on touching it.

You can also tell Flash not to automatically show the keyboard, if you plan to use your own, then you can use the focus or make your own system that would track the appropriate TextField depending on where the last touch was.

EDIT:

Here's a simplistic example of how it could be done.
In this example I have two TextFields: tf1 and tf2. Also I've added a button for the sake of covering a situation where you need to save the last focus on a concrete type of object. The buttons name is myButton.

var lastSelectedTextField:TextField; // this will hold our last selected TextField

// make sure stage exists. If you're writing script in a frame, don't mind, if you use OOP approach you can do so by adding an eventListener for the event ADDED_TO_STAGE

stage.addEventListener(FocusEvent.FOCUS_IN, onFocusIn); // onFocusIn will trigger every time a focus is changed.
myButton.addEventListener(MouseEvent.CLICK, onMyButtonClick);

function onFocusIn(e:FocusEvent):void
{
    if (stage.focus is TextField)
           lastSelectedTextField = stage.focus as TextField;
}

function onMyButtonClick(e:MouseEvent):void
{
    trace("Text of the last selected text field is:", lastSelectedTextField.text);
}

Upvotes: 1

Related Questions