ActionScript中所有输入文本字段的一个焦点事件侦听器?

时间:2013-08-15 16:57:41

标签: actionscript-3 flash textfield dynamic-text

我在安德烈亚斯的帮助下完成了它。我还添加了一些代码来获取目标文本字段的默认文本值。当我集中注意力时,这有助于我将目标字段的文本设置回默认值。谢谢“安德烈亚斯”。

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;
    }

  }
}

1 个答案:

答案 0 :(得分:0)

是的,只需引用FocusEvent中提供的displayObject,我们就可以获得对悬停对象的引用。这允许您创建对正在悬停的对象的泛型引用,最终允许您将它放在任意数量的TextField上。

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;
    }
}