2011-09-19 42 views
1

我在我的MovieClip中有一個TextArea組件。當我雙擊它時,我想切換到TextField組件,允許我更改其內容。當我單擊外部時,我想重新啓動其原始類(TextArea)。將TextArea轉換爲AS3中的TextField

我該怎麼辦?

我這樣做,但沒有奏效:

element.addEventListener(MouseEvent.DOUBLE_CLICK, changeName); 

private function changeName(e:MouseEvent):void{ 
    e.target.type = TextFieldType.INPUT; 
} 

element是一個TextArea(CLASIC和動態文本)。謝謝!

編輯:

enter image description here

這是我的影片剪輯的外觀。 「名稱」是我想允許用戶更改的TextArea。我設置這樣的:

enter image description here

[西班牙語界面]

  1. 農布雷迪奧斯instancia =實例名稱(空)
  2. Texto國家德比(經典文本)
  3. Textodinámico (動態文本)

MovieClip正在控制我的自己的基類(稱爲'ConfLayer')。裏面我有這樣的:

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元素上雙擊,以前的字符串中刪除,我不能寫什麼。

+0

您是否嘗試過使用文本區域textField屬性呢? – PatrickS

+0

@PatrickS我該如何使用它?你能舉個例子嗎?我已經與AS3新手:\ –

回答

0

我使用TextField的backgroundborder屬性解決了我自己的問題。我不是直接在我的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); 

當文本字段失去他的重點,也接受改變:

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; 
} 
0

根據我上面的評論,TextArea有一個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; 
} 
+0

謝謝,但我無法寫點東西。我會用更多信息編輯我之前的問題。 –

+0

'textfield'小寫會引發錯誤「訪問textfield屬性可能未定義」 –

+0

我的錯誤!檢查編輯後的功能... – PatrickS

0

你只後第二次點擊使得現場編輯。我的想法是遲到。該字段是可編輯的,但您需要再次單擊。因此,在第一次點擊之後嘗試做一段時間(例如300英里)的可愛時間。如果沒有點擊,則將該類型重新設置爲DYNAMIC。第二次點擊後,您將開始編輯文本字段。

+0

謝謝,但我將事件更改爲'MouseEvent.CLICK',我也無法寫入。 –