Question

i finished it with the help of Andreas. i also add some more code to get the default text value of targeted text field. that helps me to set the text of targeted field back to default when i focus out. thanks "Andreas".

import flash.events.FocusEvent;
import flash.text.TextField;

input1.addEventListener(FocusEvent.FOCUS_IN,inHand);
input1.addEventListener(FocusEvent.FOCUS_OUT,outHand);
//add all the other text input references like I did below...
input2.addEventListener(FocusEvent.FOCUS_IN, inHand);
input2.addEventListener(FocusEvent.FOCUS_OUT, outHand);

var def1:String = input1.text;
var def2:String = input2.text;

function inHand(evt:FocusEvent):void
{
var textField:TextField = TextField(evt.target);
textField.text = "";
}

function outHand(evt:FocusEvent):void
{
var textField:TextField = TextField(evt.target);
if(textField.text == "")
{
    switch(textField.name)
    {
        case "input1":
            textField.text = def1;
        break;

        case "input2":
            textField.text = def2;
        break;

        default:
        break;
    }

  }
}
Était-ce utile?

La solution

Yes, by simply referring to the displayObject provided in the FocusEvent, we can obtain a reference to the hovered object. This allows you to create generic references to the object being hovered, ultimately allowing you to place it on as many TextFields as you want.

import flash.events.FocusEvent;
import flash.text.TextField;

input1.addEventListener(FocusEvent.FOCUS_IN,inHand);
input1.addEventListener(FocusEvent.FOCUS_OUT,outHand);
//add all the other text input references like I did below...
input2.addEventListener(FocusEvent.FOCUS_IN, inHand);
input2.addEventListener(FocusEvent.FOCUS_OUT, outHand);

var def1:String = "Your text value here";

function inHand(evt:FocusEvent):void
{
    var textField:TextField = TextField(evt.target);
    textField.text = "";
}

function outHand(evt:FocusEvent):void
{
    var textField:TextField = TextField(evt.target);
    if(textField.text == "")
    {
        textField.text = def1;
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top