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
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With