<span id="Label3"></p><p><p><br></p></p><p><p>Callback-based Event Model: Event sources are unified with event snooping, and events are handled by the event source Itself.<br></p></p><p><p>Callback-based event processing is appropriate for dealing with a fixed view of the event-handling Logic.<br></p></p><p><p>Android first triggers the event listener bound on the Control.<br></p></p><p><p><br></p></p><p><p>Using a callback mechanism to handle events that occur on a GUI component requires a custom component class to inherit the GUI component class and override the Event-handling method of the class to implement it. and use the custom component class in the XML file.<br></p></p><p><p><br></p></p><p><p>Android provides some callback methods for event handling for all GUI components, such as the View Class:<br></p></p><p><p>1.boolean OnKeyDown (int keycode,keyevent event): This method is triggered when a user presses a key on the Component. Key code, event for keyboard application</p></p><p><p>2.boolean onkeylongpress (int keycode,keyevent event): This method is triggered when the user presses a key on the component long.<br></p></p><p><p>3.boolean onkeyshortcut (int keycode,keyevent event): triggers the method when a keyboard accelerator event Occurs.<br></p></p><p><p>4.boolean onKeyUp (int keycode,keyevent event): triggers the method when the user releases a key on the Component.<br></p></p><p><p>5.boolean ontouchevent (motionevent event): This method is triggered when a user triggers a touch screen event on that Component. For Touch-screen applications<br></p></p><p><p>6.boolean ontrackballevent (motionevent event): This method is triggered when the user triggers a trackball event on the Component.<br></p></p><p><p><br></p></p><p><p>When the callback method returns true, it indicates that the processing method has fully handled the event and that the event is not Propagated.<br></p></p><p><p>When the callback method returns false, indicating that the processing method does not fully handle the event, the event is Propagated.<br></p></p><p><p>For callback-based event propagation, What happens on a component not only fires the callback method on that component, it also triggers the callback method of the activity that the component is in-as long as the time can propagate to that Activity.<br></p></p><p><p><br></p></p><p><p>Example:</p></p><p><p><strong>Activity_main</strong></p></p><pre class="brush:xml;toolbar:false"><pre class="brush:xml;toolbar:false"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent "android:orien tation= "vertical" > <com.example.huidiao.mybutton android:id= "@+id/bn" android:layout_width= "wrap_c Ontent "android:layout_height=" wrap_content "android:text=" button on the touch screen "/></linearlayout></pre></pre><p><p><br></p></p><p><p><strong>Mybutton.java</strong></p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">Package Com.example.huidiao;public class MyButton extends Button {//attributeset is to receive the attribute information defined in the xml, which is not necessarily a custom layout, This property is not a custom layout, or the attribute information defined in the XML cannot be Received. Public MyButton (context context, attributeset set) {super (context, set);} The callback method for overriding the touchscreen event public boolean ontouchevent (motionevent event) {//event super.ontouchevent); LOG.V ("touch screen event on button", "touch screen on button, event source handles itself"); return false; ②}}</pre></pre><p><p><br></p></p><p><p><strong>Mainactivity.java</strong></p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">Package Com.example.huidiao;public class Mainactivity extends Activity {Button bn;protected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); setcontentview (r.layout.activity_main); bn = (Button) Findviewbyid (r.id.bn);//for bn Binding event listener Bn.setontouchlistener (new Ontouchlistener () {public boolean onTouch (View v, Motionevent Event) {//TODO auto-generated method stublog.v ("touch event on the listener button", "touch event on the listener handle button"); return false;//①}});} Override the Ontouchevent method, which listens to all controls it contains are Touch-screen events public boolean ontouchevent (motionevent event) {//events super.ontouchevent ( event); LOG.V ("touch Screen event to activity", "touch screen on custom button, event on button activity"); return false; ③}}</pre></pre><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p><p><p>This article is from "if life is just like the beginning" blog, please make sure to keep this source http://9944522.blog.51cto.com/9934522/1701798</p></p><p><p>Android (callback-based) Event handling</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service