2

当用户更改它时,我需要修改输入 TLF 文本字段中的文本。例如,每次用户添加或删除字符时,我都会尝试将其设为大写:

import fl.text.TLFTextField;
import flashx.textLayout.formats.TextLayoutFormat;
import flashx.textLayout.elements.TextFlow;

var myTLFTextField:TLFTextField = new TLFTextField();
addChild(myTLFTextField); 
myTLFTextField.x = 10;
myTLFTextField.y = 10;
myTLFTextField.width = 200
myTLFTextField.height = 100;
myTLFTextField.text = "This is my text";

var myFormat:TextLayoutFormat = new TextLayoutFormat();
myFormat.textIndent = 8;
myFormat.color = 0x336633;
myFormat.fontFamily = "Arial, Helvetica, _sans";
myFormat.fontSize = 24;

var myTextFlow:TextFlow = myTLFTextField.textFlow;
myTextFlow.hostFormat = myFormat;
myTextFlow.flowComposer.updateAllControllers();

//--

myTLFTextField.addEventListener(Event.CHANGE, this.onTextFieldChange);
function onTextFieldChange(event:Event):void
{
    myTLFTextField.text = myTLFTextField.text.toUpperCase();
}

前面的代码//--取自TLFTextField 文档,这是页面上的第一个示例。

当我尝试编辑文本时,它确实变成了大写,但之后文本字段停止响应任何输入并且输出显示

TypeError:错误 #1009:无法访问空对象引用的属性或方法。在 flashx.textLayout.container::TextContainerManager/getController() 在 flashx.textLayout.container::TextContainerManager/mouseDownHandler()

当我注释掉该addEventListener行时,一切似乎都工作正常。

这是否意味着无法像经典文本字段那样在用户输入事件上更改 TLF 文本字段中的文本?

4

3 回答 3

0

为什么?因为TLF有问题。正如其他人指出的那样,在 Event.CHANGE 处理程序中更改 TLF 文本会导致再次调用更改处理程序。到那时,事情就崩溃了。

我想出了一个类似于@Abe 发布的解决方案,但它更通用——它不依赖于检查大写字符。您侦听 TextEvent.TEXT_INPUT,然后在文本输入处理程序内切换 Event.CHANGE 侦听器。

import fl.text.TLFTextField;
import flashx.textLayout.formats.TextLayoutFormat;
import flashx.textLayout.elements.TextFlow;
import flash.text.TextFieldType;
import flash.events.TextEvent;

var myTLFTextField:TLFTextField = new TLFTextField();
addChild(myTLFTextField); 
myTLFTextField.x = 10;
myTLFTextField.y = 10;
myTLFTextField.width = 500
myTLFTextField.height = 100;
myTLFTextField.text = "This is my text";
myTLFTextField.border = true;
myTLFTextField.multiline = true;
myTLFTextField.type = TextFieldType.INPUT;

var myFormat:TextLayoutFormat = new TextLayoutFormat();
myFormat.textIndent = 8;
myFormat.color = 0x336633;
myFormat.fontFamily = "Arial, Helvetica, _sans";
myFormat.fontSize = 24;

var myTextFlow:TextFlow = myTLFTextField.textFlow;
myTextFlow.hostFormat = myFormat;
myTextFlow.flowComposer.updateAllControllers();

myTLFTextField.addEventListener(TextEvent.TEXT_INPUT, onTextInput);

var selectionIndex:uint;
function onTextInput(e:TextEvent):void
{
    trace("onTextInput");
    selectionIndex = myTLFTextField.selectionEndIndex;
    myTLFTextField.addEventListener(Event.CHANGE, onTextChanged);
}

function onTextChanged(e:Event):void
{
    trace("onTextChanged");
    myTLFTextField.removeEventListener(Event.CHANGE, onTextChanged);

    // Do whatever you need to do here:
    myTLFTextField.text = myTLFTextField.text.toUpperCase();
    myTLFTextField.setSelection(selectionIndex + 1, selectionIndex + 1);
}
于 2013-04-25T03:11:31.290 回答
0

现在无法尝试您的代码,所以这是一个疯狂的猜测,但是当您设置文本时,控制器似乎消失了。如果您愿意这样做会发生什么:

private var m_dontupdate:Boolean;
function onTextFieldChange(event:Event):void
{
  if(m_dontupdate) return;
  m_dontupdate = true;
  myTLFTextField.text = myTLFTextField.text.toUpperCase();
  myTLFTextField.textFlow.flowComposer.updateAllControllers();
  m_dontupdate = false;
}

? (我无法尝试,因为我使用 FB 4.7 并且找不到 TLFTextField)。

于 2013-04-22T12:30:10.300 回答
-1

首先-您在事件侦听器中进行无限循环!您在事件处理程序中的代码会自行调用它!TLF.text = VALUE 产生事件,如 TLF.dispatch(new Event(Event.CHANGE));

因此,将事件侦听器添加到用户操作而不是文本更改!例如对于 KEY_UP

第二 - 正确的格式代码,以便将其应用于新文本:

myTLFTextField.text = NEW_VALUE;

myTextFlow = myTLFTextField.textFlow;
myTextFlow.hostFormat = myFormat;

编辑:为了更清楚,我添加了完整的代码:

import fl.text.TLFTextField;
import flashx.textLayout.formats.TextLayoutFormat;
import flashx.textLayout.elements.TextFlow;
import flash.events.KeyboardEvent;

var myTLFTextField:TLFTextField = new TLFTextField();
addChild(myTLFTextField);
myTLFTextField.x = 10;
myTLFTextField.y = 10;
myTLFTextField.width = 400;
myTLFTextField.height = 100;
myTLFTextField.text = "This is my text";
myTLFTextField.type = "input";
//allow user to wirte in filed

var myFormat:TextLayoutFormat = new TextLayoutFormat();
myFormat.textIndent = 8;
myFormat.color = 0x336633;
myFormat.fontFamily = "Arial, Helvetica, _sans";
myFormat.fontSize = 24;

var myTextFlow:TextFlow = myTLFTextField.textFlow;
myTextFlow.hostFormat = myFormat;
myTextFlow.flowComposer.updateAllControllers();

//--;

myTLFTextField.addEventListener(Event.CHANGE, wrongHandler);
myTLFTextField.addEventListener(KeyboardEvent.KEY_UP, goodHandler);
myTLFTextField.text = 'TEXT';
//this invoke CHANGE and trace '-' in console

setTimeout(function(){  myTLFTextField.text = 'text';}, 500);
//this invoke CHANGE and trace '-' in console

function wrongHandler(event:Event):void
{
    //myTLFTextField.text = myTLFTextField.text.toUpperCase();
    //myTextFlow = myTLFTextField.textFlow;
    //myTextFlow.hostFormat = myFormat;
    // above code will run infinity loop of changing text! test it by uncomment and comment KEY_UP listener!
    trace('-'); // to see in console when and how many event was triggered
}

function goodHandler(event:Event):void
{
    myTLFTextField.text = myTLFTextField.text.toUpperCase();
    myTextFlow = myTLFTextField.textFlow; // reasign formating
    myTextFlow.hostFormat = myFormat;
    var i:uint = myTLFTextField.text.length;
    myTLFTextField.setSelection(i,i); // move carret to last sign
    trace('+'); // to see in console when and how many event was triggered
}

输出:

  1. -
  2. -

在字段中写入“a”字符后的输出:

  1. -
  2. -
  3. -
  4. -
  5. +
  6. -

舞台成绩:TEXTA

于 2013-04-22T23:55:35.523 回答