开发者

flash as3 adding focus listener to an input text field

开发者 https://www.devze.com 2023-02-22 12:57 出处:网络
Hi I\'m trying to use the following code to add a listener to a text field which clears the field, but I\'m getting the error:

Hi I'm trying to use the following code to add a listener to a text field which clears the field, but I'm getting the error:

"Parameter Listener Must be Non-Null"

This is the code I'm using:

//开发者_StackOverflowpos is the instance name of the textfield
    var posText = currCard.cardBack.pos;
    posText.addEventListener(MouseEvent.CLICK, clearText);
    posText.type = TextFieldType.INPUT;


    currCard.hit.visible = false;

    function clearText(e:MouseEvent) {
        trace ("clear");
        //posHolder = posText.text;
        //posText.text = "";
    }

I should add that this text fields starts out on stage as a dynamic text field. In the code above I am changing its type to input, so that might affect things.

I am trying to set up the field so that when a user clicks in the text field, a cursor movie clip appears, and the text that was in the field is erased, and that when they leave the text field and click elsewhere, the text is accepted, the textfield converts back to a dynamic field, essentially "locking" the entered text into the field.


var tfDynamic:TextField = new TextField( )
stage.addChild(tfDynamic )
tfDynamic.width = 100
tfDynamic.height = 20
tfDynamic.x = 10
tfDynamic.y = 10
tfDynamic.border = true
tfDynamic.type = TextFieldType.DYNAMIC;
tfDynamic.text = "hello";


tfDynamic.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut)
var savedText:String 
function onFocusOut( e:FocusEvent ):void{
    trace('onFocusOut')
    savedText =  tfDynamic.text
    tfDynamic.type = TextFieldType.DYNAMIC;

}

tfDynamic.addEventListener(MouseEvent.CLICK, onclick )
function onclick( e:MouseEvent ):void{
    trace('onclick')
    tfDynamic.text = "";
    tfDynamic.type = TextFieldType.INPUT;
}


If I had to clear the textfield, I would use FOCUS_IN. If you use onclick, when the textfield has focus and you click on it again, the textfield will be clear again... I'm not sure that what you want.

Here is the FocusEvent reference


i wouldn't recommend using "MouseEvent.CLICK" in such case, use "FocusEvent.FOCUS_OUT" instead since field might be selected using Tab key

0

精彩评论

暂无评论...
验证码 换一张
取 消