StageText issues with autocorrect and restrict

2019-06-08 07:00发布

问题:

I am attempting to use StageText in an Android AIR app as follows:

var tf:StageText = new StageText();
tf.stage = stage;
tf.viewPort = new Rectangle(0, 0, stage.fullScreenWidth, 90); 
tf.text = "Test";
tf.editable = true;
tf.autoCorrect = false;
tf.fontSize = 50;
tf.color = 0xFF0000;
tf.maxChars = 12;
tf.restrict = "A-Za-z";

When editing this StageText on an Android device everything works as expected until you hit the delete button. Until now, no autocorrect suggestions will have been displayed, but once the delete button is pressed autocorrect suggestions will now start to be displayed. Now, the next key you press will insert the autocorrected word at the cursor position instead of whatever key you pressed (e.g. if you press "s" it will insert "Son" if that was the autocorrect suggestion).

The really weird thing is that this behaviour only occurs if I set the restrict property on the StageText - if I comment out the restrict line everything functions exactly as I would expect.

回答1:

From the docs (flash.text.StageText.restrict):

Note: Restrictions apply only to user input; a script can insert any characters into the text field.

Since restrictions are not 100% reliable, instead of assigning value to the textfield's restrict property, you could evaluate the text yourself using your own logic (RegExp) in a CHANGE event handler. This would give you full control over what users enter into the textfield.

tf.addEventListener(Event.CHANGE, textFieldChangeEventHandler);

private function textFieldChangeEventHandler(event:Event):void
{
    trace(event.target.text);
    //Manage text
}