Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

as3 Textfield placeholder

I have a quick question on textfield place holder on as3.

All the modern browser or mobile apps support a place holder for the text input field. once you click on it the focus move to the beginning of the text and the place holder remain until you start typing.

Just wondering if this is archieveable on as3?

Cheers Bill

like image 853
Bill Avatar asked Jan 27 '26 09:01

Bill


2 Answers

Sure, but you need to do something with that TextField manually. The general routine is this: Have a TextField descendant class, that will have a field that will hold placeholder text.

An example:

public class PlaceholderTF extends TextField
{
    private var placeholderText:String;
    private var currentText:String;

    public function PlaceholderTF(prompt:String="Input text here") {
        super(); // making a correct TextField out of ourselves
        placeholderText = prompt;
        currentText = "";
        AWUTA = false;
        text = "";
    }

    public override function set text(value:String):void {
        if (currentText != value) {
            currentText = value;

            // calling built-in setter to correctly update text
            if (currentText == null || currentText == "") {
                super.text = placeholderText;
            } else {
                super.text = currentText;
            }
        }
    }
}

This solution is pretty crude, but might work.

like image 126
Vesper Avatar answered Jan 31 '26 07:01

Vesper


var placeholder_text:String = "Search";

myTextField.addEventListener(FocusEvent.FOCUS_IN,  focusIn);
myTextField.addEventListener(FocusEvent.FOCUS_OUT, focusOut);

// fire when click to edit
function focusIn(event:Event):void
{
    if(myTextField.text == placeholder_text)
    {
        myTextField.text = ""; 
    }
 }

// fire when you clicked out
function focusOut(event:Event):void
{

    if(myTextField.text == "")
    {
        myTextField.text = placeholder_text; 
    }
}
like image 44
psydack Avatar answered Jan 31 '26 07:01

psydack