Flex SearchInput扩展

SearchInput扩展TextInput
增加:
    imageSource属性(右侧图片);
    tipText属性(为空时出现的提示文字);
    buttonClick事件(点击button及input中敲击回车触发)。


多说无益,上代码
package {
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;

import mx.controls.Alert;
import mx.controls.Button;
import mx.controls.Image;
import mx.controls.TextInput;
import mx.events.FlexEvent;

[Event(name="buttonClick", type="flash.events.Event")]
public class SearchInput extends TextInput {
    protected var buttonImage:Button;
    private var _imageHeight:int;
    private var _imageWidth:int;

    private var _isUseTip:Boolean = false;
    private var _tipText:String = "";
    private var _tipStyle:String = "italic";
    private var _tipColor:String = "#766767";


    [Bindable]
    [Embed(source="assets/icoSearch.png")]
    private var _imageSource:Class;

    public function SearchInput() {
        super();

        this.setStyle("borderColor", "#66B3FF");
        this.addEventListener(FlexEvent.CREATION_COMPLETE, addButton);
        this.addEventListener(KeyboardEvent.KEY_DOWN, KeyEnter);
    }

    override protected function createChildren():void {
        super.createChildren();

        this._isUseTip = this._tipText != "";
        if (_isUseTip) {
            setTip();
            this.addEventListener(FocusEvent.FOCUS_IN, focusTip);
            this.addEventListener(FocusEvent.FOCUS_OUT, focusTip);
        }
    }

    private function setTip():void {
        if (this.text == "") {
            this.text = _tipText;
            this.setStyle("fontStyle", _tipStyle);
            this.setStyle("color", _tipColor);
        }
    }

    private function clearTip():void {
        if (this.getStyle("fontStyle") == _tipStyle) {
            this.text = "";
            this.setStyle("fontStyle", "");
            this.setStyle("color", "");
        }
    }

    private function focusTip(e:FocusEvent):void {
        switch (e.type) {
            case FocusEvent.FOCUS_IN :
                return clearTip();
            case FocusEvent.FOCUS_OUT :
                return setTip();
        }
    }


    private function addButton(event:FlexEvent):void {
        buttonImage = new Button();
        buttonImage.setStyle("skin", _imageSource);
        buttonImage.height = imageHeight;
        buttonImage.width = imageWidth;
        buttonImage.useHandCursor = true;
        buttonImage.move(this.width - buttonImage.width - (this.height - buttonImage.height) / 2,
                (this.height - buttonImage.height) / 2);
        buttonImage.addEventListener(MouseEvent.CLICK, buttonClick);
        this.addChild(buttonImage);
    }

    private function KeyEnter(event:KeyboardEvent):void {
        if (event.charCode != Keyboard.ENTER) return;
        this.dispatchEvent(new Event("buttonClick"));
    }

    private function buttonClick(event:MouseEvent):void {
        this.dispatchEvent(new Event("buttonClick"));
    }

    public function get imageHeight():int {
        if (_imageHeight == 0) _imageHeight = this.height;
        return _imageHeight;
    }

    public function set imageHeight(value:int):void {
        _imageHeight = value;
    }

    public function get imageWidth():int {
        if (_imageWidth == 0) _imageWidth = this.height;
        return _imageWidth;
    }

    public function set imageWidth(value:int):void {
        _imageWidth = value;
    }

    public function get imageSource():Class {
        return _imageSource;
    }

    public function set imageSource(value:Class):void {
        _imageSource = value;
    }

    public function get tipText():String {
        return _tipText;
    }

    public function set tipText(value:String):void {
        _tipText = value;
    }
}
}


你可能感兴趣的:(Flex,input,SearchInput)