In a blog text of Robert Penner (author of As3 Singal) today, Darron Schall's other blog text (Creating Default, cancelable Event handlers), Look carefully, suddenly have a kind of brief encounter feeling.
Indeed Ah ~rober Penner Singal I was not the first day to know, and then arpg the demo inside also useful, but today only serious look at the Penner of this article, but also a pity in the Schall released this article two years later only to see, really sorry good, Let's talk about the official documentation Description of these two APIs:
Public Function preventdefault (): void
If the default behavior of the event can be canceled, the behavior is canceled.
Many events have associated behaviors that are performed by default. For example, if a user types a character in a text field, the default behavior is to display the character in a text field. Because the TextEvent.TEXT_INPUT
default behavior of an event can be canceled, you can use a preventDefault()
method to prevent the character from being displayed.
An example of a non-canceled behavior is the Event.REMOVED
default behavior associated with an event, which is generated as long as Flash Player removes the display object from the display list. The preventDefault()
method has no effect on this default behavior because the default behavior cannot be canceled (the element is deleted).
You can use Event.cancelable
properties to check whether the default behavior that is associated with a particular event can be prevented. If Event.cancelable
the value is true
, it can be used preventDefault()
to cancel the event; preventDefault()
See Also
Flash.events.Event.isDefaultPrevented ()
Event.cancelable
isdefaultprevented |
() |
Method |
|
Public Function isdefaultprevented (): Boolean
Checks whether the method has been called on the event preventDefault()
. preventDefault()
returns if the method has been called true
; false
See Also
Flash.events.Event.preventDefault () about the role of the API I am not authoritative, so I pasted the official documents, and I am not very interested in Schall's article examples, so I give a practical example of what I used to do, How to implement text input limit the number of words of course, AS3 API Textfeidl provides a Maxchars API, but this is not used to know the ~ before my implementation method is to listen to TextField textevent.text_ Input event and Event.change event: When the input event is triggered, determine whether the current is legal (out of length), no longer than the length of the save a valid string change event triggered, determine whether the new character is legal, illegal, Replace the original saved legal string with the following:
Private functionInit ():void{_inputtf=NewTextField (); _inputtf.width= 100; _inputtf.border=true_inputtf.type=Textfieldtype.input; _inputtf.addeventlistener (Textevent.text_input, ontextinput); _inputtf.addeventlistener (Event.change, OnChange); This. AddChild (_INPUTTF); } Private functionOntextinput (event:textevent):void { if(Stringutils.checkstringnum (_inputtf.text) <= 6) {_tempstr=_inputtf.text; } } Private functionOnChange (event:event):void { varCurnum:int =Stringutils.checkstringnum (_inputtf.text); if(Curnum > 6) {_inputtf.text=_tempstr; } }
Now that the cancelable of the TextInput event is true, it indicates that the default behavior can be canceled by Preventdefault (the default behavior of TextField is to display text), and the new processing is:
1 Private functionInit ():void {2_inputtf =NewTextField ();3_inputtf.width = 100;4_inputtf.border =true5_inputtf.type =Textfieldtype.input;6 7 _inputtf.addeventlistener (Textevent.text_input, onTextInput2);8 9 This. AddChild (_INPUTTF);Ten } One A Private functionOnTextInput2 (event:textevent):void { - if(Stringutils.checkstringnum (_inputtf.text + event.text) > 6) { - Event.preventdefault (); the //You can also add some logic to intercept text, such as copying in 10 characters and cutting out 6 of them. - } -}
Now an event handler can be solved, and the Stringutils.checkstringnum method is also less called once, very good ~ btw: About isdefaultprevented () the use of this API, you can see schall example, Most of the implementations that are used to customize our own default event behavior are applied to, and the event that is sent out also sets Cancelable to True
Expand:1.eventdispatcher.dispatchevent () This API is returned with a Boolean return value
|
Boolean -If the event is dispatched successfully, the value is true . The value false indicates either a failure or a call to the event preventDefault() . |
The application of this article is a good idea, check it out 2. Commonly used events that can be canceled are (Cancelable is true): Focusevent.mouse_focus_change
FocusEvent.KEY_FOCUS_CHANGETextEvent.TEXT_INPUT
[Turn] two frequently ignored api:isdefaultprevented () and Preventdefault () about the event