AS3单选框实现
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
public class RadioCell extends Sprite
?? ?{
?? ??? ?public var radioMc:Mc;
?? ??? ?public var id:Object;
?? ??? ?public var radioName:String;
?? ??? ?
?? ??? ?public function RadioCell(_id:Object = null, _name:String = "", content:String = "", size:int = 14, isBold:Boolean = false)
?? ??? ?{
?? ??? ??? ?this.buttonMode = true;
?? ??? ??? ?this.useHandCursor = true;
?? ??? ??? ?if(null != _id)
?? ??? ??? ?{
?? ??? ??? ??? ?setData(_id, _name, content, size, isBold);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?public function setData(_id:Object, _name:String, content:String, size:int = 14, isBold:Boolean = false):void
?? ??? ?{
?? ??? ??? ?id = _id;
?? ??? ??? ?radioName = _name;
?? ??? ??? ?
?? ??? ??? ?radioMc = new fl_oneSelectMc();
?? ??? ??? ?this.addChild(radioMc);
?? ??? ??? ?radioMc.gotoAndStop(1);
?? ??? ??? ?
?? ??? ??? ?var htmlContent:String = "<font color='#FFCC99' size='"+size+"' face='SimSun'>"+content+"</font>";
?? ??? ??? ?if(true == isBold)
?? ??? ??? ?{
?? ??? ??? ??? ?htmlContent = "<b>"+htmlContent+"</b>";
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?var txt:TextField = this.createTxtField(htmlContent);
?? ??? ??? ?this.addChild(txt);
?? ??? ??? ?txt.x = 21;
?? ??? ??? ?
?? ??? ??? ?if(txt.textHeight + 5 > 20)
?? ??? ??? ?{
?? ??? ??? ??? ?radioMc.y = (txt.textHeight - 15)*0.5 + 2;
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?txt.y = (15 - txt.textHeight)*0.5 + 2;
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?RadioManager.getInstance().addItem(this);
?? ??? ?}
?? ??? ?
?? ??? ?public function clearSelect():void
?? ??? ?{
?? ??? ??? ?radioMc.gotoAndStop(1);
?? ??? ?}
?? ??? ?
?? ??? ?public function Select():void
?? ??? ?{
?? ??? ??? ?radioMc.gotoAndStop(2);
?? ??? ?}
?? ??? ?
?? ??? ?private function createTxtField(str:String):TextField
?? ??? ?{
?? ??? ??? ?var txtField:TextField = new TextField();
?? ??? ??? ?txtField.selectable = false;
?? ??? ??? ?txtField.width =? 120; ?
?? ??? ??? ?txtField.height =? 50;
?? ??? ??? ?txtField.autoSize = TextFieldAutoSize.LEFT;
?? ??? ??? ?txtField.htmlText = str;
?? ??? ??? ?return txtField;
?? ??? ?}
?? ?}
//管理器
public class RadioManager
{?? ??? ?private static var instance:RadioManager;
?? ??? ?
?? ??? ?private var radioTypesDict:Dictionary;
?? ??? ?private var radioValueDict:Dictionary;
?? ??? ?
?? ??? ?public function RadioManager()
?? ??? ?{
?? ??? ??? ?radioTypesDict = new Dictionary();
?? ??? ??? ?radioValueDict = new Dictionary();
?? ??? ?}
?? ??? ?
?? ??? ?public static function getInstance():RadioManager
?? ??? ?{
?? ??? ??? ?return instance = instance || new RadioManager();
?? ??? ?}
?? ??? ?
?? ??? ?/**
?? ??? ? * 添加單選項(xiàng)
?? ??? ? */?? ??? ?
?? ??? ?public function addItem(radioCell:RadioCell):void
?? ??? ?{
?? ??? ??? ?if(null == radioTypesDict[radioCell.radioName])
?? ??? ??? ?{
?? ??? ??? ??? ?radioTypesDict[radioCell.radioName] = [];
?? ??? ??? ?}
?? ??? ??? ?var arr:Array = radioTypesDict[radioCell.radioName];
?? ??? ??? ?radioCell.addEventListener(MouseEvent.CLICK, onMouseClickHandler);
?? ??? ??? ?arr[arr.length] = radioCell;
?? ??? ?}
?? ??? ?
?? ??? ?private function onMouseClickHandler(e:MouseEvent):void
?? ??? ?{
?? ??? ??? ?var radioCell:RadioCell = e.currentTarget as RadioCell;
?? ??? ??? ?//已選的就不再遍歷了
?? ??? ??? ?if(radioValueDict[radioCell.radioName] == radioCell)
?? ??? ??? ?{
?? ??? ??? ??? ?return;
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?//上一次選中的去掉
?? ??? ??? ?if(null != radioValueDict[radioCell.radioName])
?? ??? ??? ?{
?? ??? ??? ??? ?(radioValueDict[radioCell.radioName] as RadioCell).clearSelect();
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?//選中這次點(diǎn)擊
?? ??? ??? ?radioCell.Select();
?? ??? ??? ?radioValueDict[radioCell.radioName] = radioCell;
?? ??? ?}?? ??? ?
?? ??? ?
?? ??? ?/**
?? ??? ? * 獲得單選框的值
?? ??? ? * @param _name 類型名
?? ??? ? * @return
?? ??? ? */?? ??? ?
?? ??? ?public function getSelectedRadioId(_name:String):Object
?? ??? ?{
?? ??? ??? ?return (radioValueDict[_name] as RadioCell).id;
?? ??? ?}
?? ??? ?
?? ??? ?/**
?? ??? ? * 設(shè)置默認(rèn)選中
?? ??? ? * @param _name 類型
?? ??? ? * @param id 標(biāo)識(shí)
?? ??? ? */?? ??? ?
?? ??? ?public function setDefaultSelectedRadio(_name:String, id:Object):void
?? ??? ?{
?? ??? ??? ?//遍歷
?? ??? ??? ?for each(var cell:RadioCell in radioTypesDict[_name])
?? ??? ??? ?{
?? ??? ??? ??? ?if(cell.id == id)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?cell.Select();
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?/**
?? ??? ? * 設(shè)置選框不可點(diǎn)
?? ??? ? */?? ??? ?
?? ??? ?public function disableRadioType(_name:String):void
?? ??? ?{
?? ??? ??? ?for each(var cell:RadioCell in radioTypesDict[_name])
?? ??? ??? ?{
?? ??? ??? ??? ?cell.buttonMode = false;
?? ??? ??? ??? ?cell.useHandCursor = false;
?? ??? ??? ??? ?cell.mouseEnabled = false;
?? ??? ??? ??? ?cell.removeEventListener(MouseEvent.CLICK, onMouseClickHandler);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?/**
?? ??? ? * 設(shè)置選框可點(diǎn)
?? ??? ? */?? ??? ?
?? ??? ?public function ableRadioType(_name:String):void
?? ??? ?{
?? ??? ??? ?for each(var cell:RadioCell in radioTypesDict[_name])
?? ??? ??? ?{
?? ??? ??? ??? ?cell.buttonMode = true;
?? ??? ??? ??? ?cell.useHandCursor = true;
?? ??? ??? ??? ?cell.mouseEnabled = true;
?? ??? ??? ??? ?cell.addEventListener(MouseEvent.CLICK, onMouseClickHandler);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ?}
轉(zhuǎn)載于:https://my.oschina.net/u/185335/blog/179800
總結(jié)
- 上一篇: 高可用MySQL架构设计2
- 下一篇: 关闭fedroa19开关机画面