Flash / Flex / ActionScript/TextField/text event
Содержание
Calling ActionScript from Hyperlinks
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.*;
public class Main extends Sprite {
public function Main( ) {
var field:TextField = new TextField( );
field.htmlText = "<a href="event:http://www.wbex.ru">Website</a>";
field.addEventListener(TextEvent.LINK, onClick);
addChild(field);
}
private function onClick(event:TextEvent):void {
trace("click");
}
}
}
Listening to TextField Events
textInput Event
package
{
import flash.text.TextField;
import flash.display.Sprite;
import flash.events.TextEvent;
public class Main extends Sprite
{
private var inputText:TextField;
private var regex:RegExp;
private var dict:Array;
public function Main()
{
dict = new Array("flash", "java", "C#", "python", "ruby", "C++");
inputText = new TextField();
inputText.type = "input";
inputText.addEventListener(TextEvent.TEXT_INPUT, predictText);
inputText.border = true;
addChild(inputText);
}
private function predictText(textEvent:TextEvent):void
{
this.regex = new RegExp(textEvent.text, "i");
for each(var term:String in dict)
{
if(regex.test(term))
{
inputText.text = term.substring(textEvent.text.length);
}
}
}
}
}
Prevent Default Test
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.TextEvent;
[SWF(width=550, height=400)]
public class Main extends Sprite {
public function Main() {
var tf:TextField = new TextField();
addChild(tf);
tf.width = stage.stageWidth;
tf.height = stage.stageWidth;
tf.type = TextFieldType.INPUT;
tf.wordWrap = true;
tf.addEventListener(TextEvent.TEXT_INPUT, onTextFieldTextInput);
}
private function onTextFieldTextInput(event:TextEvent):void {
var tf:TextField = event.target as TextField;
if (tf.text.indexOf(event.text) > -1) {
event.preventDefault();
}
}
}
}
Responding to User Text Entry: field.addEventListener(TextEvent.TEXT_INPUT, onTextInput);
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.TextEvent;
import flash.events.TextEvent;
public class Main extends Sprite {
private var _field:TextField = new TextField( );
public function Main( ) {
_field.border = true;
_field.background = true;
_field.type = TextFieldType.INPUT;
addChild(_field);
_field.addEventListener(TextEvent.TEXT_INPUT, onTextInput);
}
private function onTextInput(event:TextEvent):void {
if(event.text == "a" && _field.length == 0) {
event.preventDefault( );
}
}
}
}