我试图测试文本输入框是否具有焦点

时间:2011-10-11 03:38:44

标签: actionscript-3 testing

我对AS3很新,所以这是我的问题。

我有两个文本输入框p1和p2。我想做一个条件测试,看看p1是否有焦点。如果没有,则p2必须具有焦点。这是我试图开始工作的一些代码。

if ((Selection.getFocus()) == (p1totalScore.text)){
    p1Score();
}   p2Score();

感谢您的帮助。

大卫

2 个答案:

答案 0 :(得分:2)

如果没有FocusManager,您可以测试舞台返回焦点的位置:

(如果在艺术板上有textInput1和textInput2)

import flash.events.MouseEvent;
import fl.controls.TextInput;

var textInput1:TextInput;
var textInput2:TextInput;

stage.addEventListener(MouseEvent.CLICK, mouseClickHandler);

function mouseClickHandler(event:MouseEvent):void
{
    if(stage.focus == textInput1.textField)
        trace("text field 1 has focus.");
    else if(stage.focus == textInput2.textField)
        trace("Text field 2 has focus.");
}

我认为比您尝试更好的方法是为焦点更改添加事件处理程序:

import fl.controls.TextInput;
import fl.managers.FocusManager;
import flash.events.FocusEvent;

var textInput1:TextInput;
var textInput2:TextInput;

var focusManager:FocusManager = new FocusManager(this);

textInput1.addEventListener(FocusEvent.FOCUS_IN, textInput1FocusHandler);
textInput2.addEventListener(FocusEvent.FOCUS_IN, textInput2FocusHandler);

function textInput1FocusHandler(event:FocusEvent):void
{
    trace("textInput1 has focus.");
}

function textInput2FocusHandler(event:FocusEvent):void
{
    trace("textInput2 has focus.");
}

答案 1 :(得分:0)

Adob​​e将获得99%的支持: 'http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/managers/FocusManager.html'

我刚刚更改了函数以返回具有焦点的控件的名称,而不是其示例返回的实例名称。

private function focusChange(e:FocusEvent):void {
var ti_now:InteractiveObject;
ti_now = fm.getFocus();
trace("Focus now: ", ti_now.name);
}