制作删除按钮以删除 inputtext 字段中的文本
Posted
技术标签:
【中文标题】制作删除按钮以删除 inputtext 字段中的文本【英文标题】:Making a delete button to delete text in the inputtext field 【发布时间】:2011-05-08 20:36:57 【问题描述】:当我尝试在舞台上创建一个按钮以删除我在 inputtext(ti
) 中输入的文本时,我遇到了这四个错误。根据我的脚本和错误,我应该写什么来创建删除按钮?
尝试删除固定属性 文本。仅动态定义 属性可以删除。 访问 未定义的属性 delete_btn。 使用权 可能未定义的属性 buttonDown 通过一个参考 静态类型类。 警告:3600:声明的属性文本不能 删除。要释放相关的内存, 将其值设置为 null。
delete_btn.addEventListener(MouseEvent.buttonDown, deletetxt);
function deletetxt(event:TextEvent):void
delete ti.text
ti.border = true
ti.addEventListener(TextEvent.TEXT_INPUT, onInput);
function onInput(event:TextEvent):void
if(ti.text.search('a')!=-1) load_image("http://i54.tinypic.com/anom5d.png", "ottefct");
else if(ti.text.search('b')!=-1) load_image("http://i53.tinypic.com/2dv7dao.png", "rnd");
else if(ti.text.search('c')!=-1) load_image("http://i51.tinypic.com/m8jp7m.png", "ssd");
var loaded_images:Dictionary = new Dictionary();
function load_image(url:String, id_name:String)
var loader:Loader = new Loader();
loader.name = id_name;
var url_req:URLRequest = new URLRequest(url);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
loader.load(url_req);
function onLoadingComplete(evt:Event):void
var img_name:String = evt.currentTarget.loader.name
var spr_box:Sprite = new Sprite();
spr_box.addChild(evt.currentTarget.loader);
spr_box.mouseChildren = false;
spr_box.doubleClickEnabled = true;
spr_box.addEventListener(MouseEvent.MOUSE_DOWN, drag);
spr_box.addEventListener(MouseEvent.MOUSE_UP, drop);
spr_box.addEventListener(MouseEvent.MOUSE_WHEEL, rotate);
spr_box.addEventListener(MouseEvent.DOUBLE_CLICK , unrotate);
spr_box.width = 124;
spr_box.height = 180;
this.addChild(spr_box);
loaded_images[img_name] = spr_box;
function drag(evt:MouseEvent):void
evt.currentTarget.startDrag()
function drop(evt:MouseEvent):void
evt.currentTarget.stopDrag()
function rotate(evt:MouseEvent):void
evt.currentTarget.rotation = 90
function unrotate(evt:MouseEvent):void
evt.currentTarget.rotation = 0
【问题讨论】:
【参考方案1】:你有几个错误!
尝试删除固定属性文本。只能删除动态定义的属性。 和 警告:3600:无法删除声明的属性文本。要释放关联的内存,请将其值设置为 null。
要删除文字时出现错误:
function deletetxt(event:TextEvent):void
delete ti.text; // <-- Error HERE!
你应该这样做:
function deletetxt(event:MouseEvent):void // <-- Sorry, didn't see the "TextEvent"
ti.text = "";
delete
关键字用于其他事情(例如删除字典条目)
未定义属性 delete_btn 的访问权限
这里您没有创建“delete_btn”按钮(或者它有另一个实例名称)通过静态类型 Class 的引用访问可能未定义的属性 buttonDown
MouseEvent.buttonDown
不存在,也许你想用MouseEvent.CLICK
或MouseEvent.MOUSE_DOWN
代替
【讨论】:
我现在收到此错误。TypeError:错误 #1034:类型强制失败:无法将 flash.events::MouseEvent@1edf37c1 转换为 flash.events.TextEvent。 是的,抱歉,我没有看到 TextEvent,应该是 MouseEvent(或者只是 Event)【参考方案2】:delete_btn.addEventListener(MouseEvent.CLICK, deletetxt);
function deletetxt(event:Event):void
ti.text = "";
Unkiwii 对 ti.text = "" 的看法是正确的,但强制无法转换事件,所以我只是将其设为事件:事件
【讨论】:
您应该将 event:Event 更改为 event:MouseEvent以上是关于制作删除按钮以删除 inputtext 字段中的文本的主要内容,如果未能解决你的问题,请参考以下文章
当用户以编程方式单击swift ios中的删除按钮时,如何将光标从一个文本字段自动移动到另一个文本字段?