AS3: TextField Focus

断了今生、忘了曾经 提交于 2019-12-07 22:53:43

问题


I'm trying to handle a focus event on a TextField so I can select all the text when focusing (tab or click). Seems like I'm doing something wrong here ?

txtTextField.addEventListener(FocusEvent.FOCUS_IN, handleFocusIn);
function handleFocusIn() {
 //select all text here
}

回答1:


I needed the same thing, to select the contents of a textfield when it receives focus.

I tried:

A) Simply selecting after a FocusEvent. This doesn't seem to work (my guess is that FocusEvents are fired before the mouse click is being processed, which in turn will undo the selection).

B) Selecting on every mouse click. This works, but this is very annoying for a user who wants to select only a part of the text later, since this attempt will always result in -all- the content being selected.

The following workaround seems to work though:

    myTextField.addEventListener(MouseEvent.CLICK, selectAllOnce);

    function selectAllOnce(e:MouseEvent) {
        e.target.removeEventListener(MouseEvent.CLICK, selectAllOnce);
        e.target.addEventListener(FocusEvent.FOCUS_OUT, addSelectListener);
        selectAll(e);
    }

    function addSelectListener(e:FocusEvent) {
        e.target.addEventListener(MouseEvent.CLICK, selectAllOnce);
        e.target.removeEventListener(FocusEvent.FOCUS_OUT, addSelectListener);
    }

    function selectAll(e:Event) {
        e.target.setSelection(0, e.target.getLineLength(0));
    }

Hope that helps. I personally think it would be most logical if adobe simply added an option for this for the TextField object.




回答2:


Your handleFocusIn should have the signature

function handleFocusIn(event:FocusEvent) // or just Event



回答3:


I had a similar problem at the prototype phase of a development (in Flash). A textfield wasn't firing FocusEvent.FOCUS_OUT events at all. The problem was i had a Button component on the stage. As soon as i replaced that flash Button component instance with a custom button a created from scratch, i got it to work. I haven't been able to find this bug and the solution over the internet.

With a Button component on stage i get FOCUS_IN event only the first time i click on it. After that i don't get neither FOCUS_OUT nor FOCUS_IN events fired.

I hope this would help someone in any way.




回答4:


I am doing my handler like this. Works like a charm:

private function onFocusIn(e:FocusEvent):void 
{
    setTimeout(title.setSelection, 100, 0, e.target.text.length);           
}



回答5:


you can find user isn't selecting only part of text like this:

private function clickHandler(e:MouseEvent){
    if(tfield.selectionBeginIndex == tfield.selectionEndIndex){
        tfield.setSelection(0, tfield.length);
    }
}



回答6:


The simple solution is:

import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.FocusEvent;

tf.addEventListener(FocusEvent.FOCUS_IN, handleFocusIn);

function handleFocusIn(e:FocusEvent):void
{
    tf.addEventListener(MouseEvent.MOUSE_UP, preventLosingSelection);
    tf.setSelection(0, tf.length);
    stage.focus = tf;
}

function preventLosingSelection(e:MouseEvent):void
{
    tf.removeEventListener(MouseEvent.MOUSE_UP, preventLosingSelection);
    tf.setSelection(0, tf.length);
    stage.focus = tf;
}

Hope this help for someone in future!




回答7:


You simply forgot the var that catches the event. It should be between the brackets in the function definition. The shortest way is just to call it "e" without further definitions, as I did here. It works:

txtTextField.addEventListener(FocusEvent.FOCUS_IN, handleFocusIn);
function handleFocusIn(e) {
 //select all text here
}


来源:https://stackoverflow.com/questions/1109220/as3-textfield-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!