Actionscript 3 在AS3中将TextArea转换为TextField
我的MovieClip中有一个TextArea组件。当我双击它时,我想切换到TextField组件,允许我更改它的内容。当我单击outside时,我想重新启动它的原始类(TextArea) 我怎么做 我正在这样做,但没有成功:Actionscript 3 在AS3中将TextArea转换为TextField,actionscript-3,flash-cs5,Actionscript 3,Flash Cs5,我的MovieClip中有一个TextArea组件。当我双击它时,我想切换到TextField组件,允许我更改它的内容。当我单击outside时,我想重新启动它的原始类(TextArea) 我怎么做 我正在这样做,但没有成功: element.addEventListener(MouseEvent.DOUBLE_CLICK, changeName); private function changeName(e:MouseEvent):void{ e.target.type = TextF
element.addEventListener(MouseEvent.DOUBLE_CLICK, changeName);
private function changeName(e:MouseEvent):void{
e.target.type = TextFieldType.INPUT;
}
其中元素
是一个文本区域(基本文本和动态文本)。谢谢
编辑:
这就是我的电影唇的样子。“Name”是我希望允许用户更改的文本区域。我是这样设置的:
private function changeName(e:MouseEvent):void{
var tarea:TextArea = e.target as TextArea;
var tf:TextField = tarea.textField; //make sure that textfield is camelCase!!!
tf.type = TextFieldType.INPUT;
}
[西班牙语界面]
public function doStuff(e:MouseEvent):void{
// element = TextArea 'Name'
element.addEventListener(MouseEvent.DOUBLE_CLICK, changeName);
}
private function changeName(e:MouseEvent):void {
var tarea:TextArea = e.target as TextArea;
var tf:TextField = tarea.TextField; // this line throwing error
tf.type = TextFieldType.INPUT;
}
因为AS3给了我错误,我尝试了以下方法:
private function changeName(e:MouseEvent):void {
e.target.TextField.type = TextFieldType.INPUT;
}
当我双击TextArea元素时,前面的字符串将被删除,我无法写入任何内容。在我上面的评论之后,a有一个textfield属性 因此,您应该能够这样做: private function changeName(e:MouseEvent):void{ var tarea:TextArea = e.target as TextArea; var tf:TextField = tarea.textField; //make sure that textfield is camelCase!!! tf.type = TextFieldType.INPUT; } 私有函数changeName(e:MouseEvent):void{ var tarea:TextArea=e.target作为TextArea; var tf:TextField=tarea.TextField;//确保TextField是camelCase!!! tf.type=TextFieldType.INPUT; }
只有在第二次单击后,才能使字段可编辑。我的想法是太晚了。该字段是可编辑的,但您需要再次单击。因此,尝试在第一次单击后使其可供食用一段时间(例如300毫秒)。如果没有单击,则将类型设置回“动态”。第二次单击后,您将开始编辑文本字段。我使用TextField中的
background
和border
属性解决了自己的问题。我不是直接在MovieClip上创建TextField,而是动态创建它
// Field 'Name' of the layer
var tarea:TextField = new TextField();
tarea.text = 'Layer';
tarea.x = -80;
tarea.y = -23;
tarea.type = TextFieldType.DYNAMIC;
tarea.height = 20;
// Format the TextField
var format_tarea:TextFormat = new TextFormat();
format_tarea.font = "Arial";
format_tarea.size = 14;
format_tarea.bold = true;
tarea.setTextFormat(format_tarea,0,tarea.length);
然后,我将其添加到一些侦听器中,以允许在单击时进行更改:
// Add event to allow name changes
tarea.addEventListener(MouseEvent.CLICK,changeName);
当我按ENTER键时,我接受更改
// Add event to accept changes on press ENTER key
tarea.addEventListener(KeyboardEvent.KEY_DOWN,acceptKeyboardName);
当TextField失去焦点时,也接受更改:
tarea.addEventListener(FocusEvent.FOCUS_OUT,acceptFocusName);
最后,我在我自己的电影里加入了
// Add to MC
mc.addChild(tarea);
与以下事件关联的处理程序如下所示:
private function changeName(e:MouseEvent):void
{
e.target.setSelection(0,e.target.text.length);
e.target.border = true;
e.target.background = true;
e.target.type = TextFieldType.INPUT;
}
private function acceptKeyboardName(e:KeyboardEvent):void
{
if (e.charCode == 13) // When pres ENTER, remove border, background and restore dynamic type
{
e.target.type = TextFieldType.DYNAMIC;
e.target.border = false;
e.target.background = false;
}
}
private function acceptFocusName(e:FocusEvent):void
{
// When lost focus, remove border, background and restore dynamic type
e.target.type = TextFieldType.DYNAMIC;
e.target.border = false;
e.target.background = false;
}
您尝试过使用TextArea textfield属性吗?@Patrick我如何使用它?你能举个例子吗?我已经是AS3:\的新手了,谢谢,但是我不能写一些东西。我将用更多信息编辑我的上一个问题。
textfield
小写字母提示我一个错误“对textfield属性的访问可能未定义”。谢谢,但我已将事件更改为MouseEvent。单击
,我也无法写入。