3

我对 as3 上的文本字段占位符有一个简短的问题。

所有现代浏览器或移动应用程序都支持文本输入字段的占位符。单击它后,焦点将移至文本的开头,并且占位符将一直保留,直到您开始键入为止。

只是想知道这是否可以在 as3 上归档?

干杯比尔

4

3 回答 3

3

当然可以,但是您需要手动对该 TextField 进行操作。一般的例程是这样的:有一个 TextField 后代类,它将有一个保存占位符文本的字段。

一个例子:

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;
            }
        }
    }
}

这个解决方案非常粗糙,但可能会奏效。

于 2012-11-05T04:51:40.900 回答
3
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; 
    }
}
于 2013-02-19T13:47:09.303 回答
0

Here is an example that works for me with form validation:

package components {
import flash.events.Event;
import flash.events.FocusEvent;
import flash.text.TextField;

public class TextFieldPlaceholder extends TextField {

    private var _placeholderText:String;
    private var _currentText:String;

    public function TextFieldPlaceholder(placeholderText:String = 'Input text here') {
        _placeholderText = placeholderText;
        _currentText = '';
        this.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
        this.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
        super();
    }

    override public function get text():String {
        if (_currentText == _placeholderText) {
            return '';
        }
        return _currentText;
    }

    override public function set text(value:String):void {
        _currentText = value;
        if (!_currentText) {
            super.text = _placeholderText;
        } else {
            super.text = _currentText;
        }
    }

    private function onFocusIn(e:Event):void {
        super.text = _currentText;
    }

    private function onFocusOut(e:Event):void {
        this.text = super.text;
    }
  }
}
于 2018-08-20T05:28:04.900 回答