0

我有一个文本字段,按下按钮后应该可以编辑:

playerName = new TextField();
playerName.autoSize = "left";
playerName.styleSheet = styleSheet;
playerName.type = TextFieldType.DYNAMIC;
playerName.tabEnabled = false;
playerName.maxChars = 14;
playerName.background = false;
playerName.backgroundColor = 0x1e904b;
...
var boldStyleObject:Object = {};
boldStyleObject.fontSize = 12;
boldStyleObject.fontFamily = "Verdana";
boldStyleObject.color = "#ffffff";
boldStyleObject.fontWeight = "bold";
styleSheet.setStyle(".bold", boldStyleObject);
boldStyleName = "bold";
...
playerName.htmlText = "<span class='" + boldStyleName + "'>" + player.name + "</span>";
...
private function ChangeName (event:Event):void
{
    playerName.type = TextFieldType.INPUT;
    playerName.background = true;
    playerName.addEventListener(KeyboardEvent.KEY_DOWN, DoneEditing);
    stage.focus = playerName;
}
...

private function DoneEditing (event:KeyboardEvent):void
{
    var target:TextField = event.target as TextField;

    if (event.keyCode == 27)
    {
        target.text = editBackup;
        DisactivateTextField(target);
    }
    else if (event.keyCode == 13 && target == playerName && playerName.text.length > 3)
    {
        DisactivateTextField(target);
    }
}

private function DisactivateTextField(field:TextField):void
{
    field.type = TextFieldType.DYNAMIC;
    field.background = false;
    field.removeEventListener(KeyboardEvent.KEY_DOWN, DoneEditing);
}

一切正常,除了 TextField 的可编辑性。为什么?

4

2 回答 2

1

按下按钮需要一个MouseEvent,因此请确保您将正确的事件侦听器添加到您的按钮。此外,请确保您正在检查用户输入以终止编辑或评估输入本身。

private function something():void {
    yourBtn.addEventListener(MouseEvent.CLICK,ChangeName);
}
private function ChangeName(e:MouseEvent):void {
    playerName.type = TextFieldType.INPUT;
    playerName.background = true;
    playerName.addEventListener(KeyboardEvent.KEY_DOWN,DoneEditing);
    stage.focus = playerName;
}
private function DoneEditing(e:KeyboardEvent):void {
    if (e.keyCode == Keyboard.ENTER) {
        // do your magic here
    }
}
于 2012-12-25T05:12:55.570 回答
0

问题出在html样式中。出于某种原因,TextField无法编辑通过 styleSheet 设置样式的 htmlText。您应该使用TextField.defaultTextFormat属性来设置要编辑的文本样式。

于 2012-12-25T08:48:02.187 回答