写步进器的时候,一般要求TextField也能支持输入,而且是大于等于0的有意义的数字 , 比如 "01"就是不行的.在这里需要介绍一点知识 , (本着不知道知道,知道理解的宗旨)
事件 :
TextEvent.TEXT_INPUT
Event.CHANGE
首先 , TextEvent.TEXT_INPUT : 有文字将要输入 , 而且是一个一个的输入(待会解释)
Event.CHANGE : 文本输入后触发
对于 : TextField , 有一个只读的属性 : careIndex , 其意义是 : 光标插入点位置的索引
好了 , 上一个步进器的是实现:
package com.niuniu.module.room.view.item
{
import com.game.common.Config;
import com.game.common.GameUtil;
import com.game.common.StageInfo;
import com.game.moduleBase.view.BaseView;
import com.game.moduleCall.ModuleCallManger;
import com.game.ui.layer.LayerManager;
import com.game.ui.layer.LayerType;
import com.game.ui.reslib.ResLib;
import com.niuniu.GameEventTypeStr;
import com.niuniu.common.UserManager;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TextEvent;
import flash.text.TextField;
import flash.text.TextFieldType;
import tw.ui.AlertDialog;
/**
* 带入筹码Panel
* @author Kayer
* */
public final class BuyChipsPanel extends BaseView
{
private var $btnClose : SimpleButton;
private var $btnMinus : SimpleButton;
private var $btnPlus : SimpleButton;
private var $btnSure : SimpleButton;
private var $labValue : TextField;
private var $bgMask:Sprite;//背景遮罩
private var $isMask:Boolean;
private var $stepOff : uint = 1;
/**本次最多能购买的筹码数*/
private var $maxBuyingChips : uint = 0;
/**文本*/
private var $text : String;
/**
* @param $isMask : 是否是模态
* @param $stepOff : 步进器步长
* */
public function BuyChipsPanel( $isMask : Boolean = true , $stepOff : uint = 1 )
{
this.$isMask = $isMask;
this.$stepOff = $stepOff;
this.$maxBuyingChips = Config.maxChipsBuyWhthMoBean( UserManager.instance.mySelfMoBean );
super();
}
override protected function initDis():void
{
if( this.$isMask )
{
this.$bgMask = new Sprite();
LayerManager.instance.getLayer(LayerType.LAYER_POPUP_DIALOG).addChild(this.$bgMask);
}
this._mc = ResLib.getMovieClip(‘com.niuniu.module.room.BuyChips‘);//com.niuniu.module.room.ROOM_VIEW
this.addChild( this.mc );
this.$btnClose = this.mc["btnClose"];
this.$btnMinus = this.mc["btnMinus"];
this.$btnPlus = this.mc["btnPlus"];
this.$btnSure = this.mc["btnSure"];//btnSure
this.$labValue = this.mc["labValue"];
this.$labValue.type = TextFieldType.INPUT;
// this.$labValue.mouseEnabled = false;
this.$labValue.text = "0";
super.makeEnabled( true );
}
override public function onStageResizeListener(e:Event):void
{
if( this.$isMask )
{
if ( this.$bgMask != null ) {
this.$bgMask.graphics.clear();
this.$bgMask.graphics.beginFill( 0x000000 , 0.2 );
this.$bgMask.graphics.drawRect(0,0,StageInfo.STAGE.stageWidth,StageInfo.STAGE.stageHeight);
this.$bgMask.graphics.endFill();
}
}
this.setXY( (StageInfo.WIDTH - this.width ) >>1 , ( StageInfo.HEIGHT - this.height ) >>1 );
}
override protected function initEvent():void
{
this.$btnClose.addEventListener( MouseEvent.CLICK , onClickHandler );
this.$btnMinus.addEventListener( MouseEvent.CLICK , onClickHandler );
this.$btnPlus.addEventListener( MouseEvent.CLICK , onClickHandler );
this.$btnSure.addEventListener( MouseEvent.CLICK , onClickHandler );
this.$labValue.addEventListener( TextEvent.TEXT_INPUT , onChipsNumChange );
this.$labValue.addEventListener( Event.CHANGE , onChipsNumChangeA );
StageInfo.STAGE.addEventListener(Event.RESIZE, onStageResizeListener);
}
override protected function removeEvent():void
{
this.$btnClose.removeEventListener( MouseEvent.CLICK , onClickHandler );
this.$btnMinus.removeEventListener( MouseEvent.CLICK , onClickHandler );
this.$btnPlus.removeEventListener( MouseEvent.CLICK , onClickHandler );
this.$btnSure.removeEventListener( MouseEvent.CLICK , onClickHandler );
this.$labValue.removeEventListener( TextEvent.TEXT_INPUT , onChipsNumChange );
this.$labValue.removeEventListener( Event.CHANGE , onChipsNumChangeA );
StageInfo.STAGE.addEventListener(Event.RESIZE, onStageResizeListener);
}
private function onClickHandler( $e : MouseEvent ) : void
{
switch( $e.target.name )
{
case "btnClose":
this.destroy();
break;
case "btnMinus":
trace("btnMinus");
this.stepHandlder( - this.$stepOff );
break;
case "btnPlus":
trace("btnPlus");
this.stepHandlder( this.$stepOff );
break;
case "btnSure":
trace("btnSure");
this.onSure();
break;
}
}
private function onChipsNumChange( $e : TextEvent ) : void
{
if( this.$labValue.text.substr(0,1) == "0" && ($e.currentTarget as TextField).caretIndex > 0 )
{
this.$text = this.$labValue.text;
}
else
{
if( isNaN( Number($e.text) ) )
this.$text = this.$labValue.text;
else if( int( $e.text ) == 0 )
{
if( ($e.currentTarget as TextField).caretIndex == 0 )
this.$text = this.$labValue.text;
else
this.$text = "";
}
else
this.$text = "";
}
}
private function onChipsNumChangeA( $e : Event ) : void
{
if( this.$text != "" )
this.$labValue.text = this.$text;
else
{
var buffer : Number = Number( this.$labValue.text );
if( buffer > this.$maxBuyingChips )
this.$labValue.text = this.$maxBuyingChips.toString();
}
}
/**
* 显示
* */
public function show() : void
{
onStageResizeListener(null);
LayerManager.instance.getLayer(LayerType.LAYER_POPUP_DIALOG).addChild(this);
}
/**
* 处理兑换
* */
private function onSure() : void
{
if( uint( GameUtil.trim(this.$labValue.text ) ) <= 0 )
{
AlertDialog.createWarnWin( "" , "请重新设置兑换数额!", 0 , AlertDialog.VIEWOK , function( $e : String ) : void{ } , true );
}
else
{
ModuleCallManger.disE( GameEventTypeStr.NOTYFY_MAGICBEAN_2_CHIPS_REQ , uint( GameUtil.trim(this.$labValue.text ) ) );
this.destroy();
}
}
/**
* 实现步进器算法
* @param $off : (+- 步进器步长)
* */
private function stepHandlder( $off : int ) : void
{
var buffer : Number = Number( this.$labValue.text ) + $off;
if( buffer >= 0 )
{
if( buffer <= this.$maxBuyingChips )
this.$labValue.text = buffer.toString();
else
{
this.$labValue.text == this.$maxBuyingChips.toString();
}
}
}
override protected function removeDis():void
{
if( this.$isMask )
{
if( this.$bgMask != null )
{
LayerManager.instance.getLayer(LayerType.LAYER_POPUP_DIALOG).removeChild(this.$bgMask);
this.$bgMask = null;
}
}
}
override public function destroy():void
{
super.destroy();
}
}
}
现在需要解释 : TextEvent.TEXT_INPUT , 一个一个的输入"woshi"时
第一次获取 : "w" , 第二次获取 "o" , 由于TextEvent.TEXT_INPUT : 有文字将要输入 ( target(TextField)的值没变,光标没变 ) ,所以 , careIndex 一直是1(在默认值"0"的后面输入)
本文出自 “Better_Power_Wisdom” 博客,转载请与作者联系!
原文:http://aonaufly.blog.51cto.com/3554853/1828728