[Change] Android event listener callback methods of

Android event listener interface, as view View class, which contains a number of callback methods, such as: onClick (); onLongClick (); onFocusChange (); onKey (); onTouch (); onCreateContextMenu () and so on.

Android The operating system, for the event handling is a very basic and important operation. The realization of many of the features needed to trigger the relevant events in order to achieve their own objectives. Such as Android event listener is a view View class interface that contains a single callback method. These methods will be in the view of the listener has been registered in the user interface action is triggered by the Android framework of the call. The following callback methods are included in the Android event listener interface:

onClick ()

Included in the View.OnClickListener. When the user touch the item (in the touch mode), or by browsing key or trackball focus on this item, then press the "confirm" button or press the track ball is called.

onLongClick ()

Included in the View.OnLongClickListener. When the user touch and do something about the item (in the touch mode), or by browsing key or trackball focus on this item, and then keep pressing the "confirm" button or press the track ball (one second) is called.

onFocusChange ()

Included in the View.OnFocusChangeListener. When the user use the browse button or follow the ball here when you enter or leave the item is called.

onKey ()

Included in the View.OnKeyListener. When the user focus on this item and press or release a button when the device is called.

onTouch ()

Included in the View.OnTouchListener. When the user performs the action be treated as a touch event is called, including the press, release, or move any gestures on the screen (within the boundary of this item).

onCreateContextMenu ()

Included in the View.OnCreateContextMenuListener. When is creating a context menu when they were called (as a continuous "long click" action of the results). See the section Creating Menus Creating the menu for more information.

These methods are their corresponding interface, the only "household." To define these methods and handle your event, in your activities to achieve this nested interface or define it as an anonymous class. Then, pass an instance of your implementation to the respective View.set ... Listener () method. (Eg, call setOnClickListener () and pass to it your OnClickListener met).

The following example shows how to register a click on a button listener:

  1. / / Create an anonymous implementation of OnClickListener
  2. private OnClickListener mCorkyListener = new OnClickListener () (
  3. public void onClick (View v) (
  4. / / Do something when the button is clicked
  5. )
  6. );
  7. protected void onCreate (Bundle savedValues) (
  8. ...
  9. / / Capture our button from layout
  10. Button button = (Button) findViewById (R.id.corky);
  11. / / Register the onClick listener with the implementation above
  12. button.setOnClickListener (mCorkyListener);
  13. ...
  14. )

You may find the OnClickListener as part of the activities to achieve would be more convenient. This will avoid additional class loading and object distribution. For example:

  1. public class ExampleActivity extends Activity implements OnClickListener (
  2. protected void onCreate (Bundle savedValues) (
  3. ...
  4. Button button = (Button) findViewById (R.id.corky);
  5. button.setOnClickListener (this);
  6. )
  7. / / Implement the OnClickListener callback
  8. public void onClick (View v) (
  9. / / Do something when the button is clicked
  10. )
  11. ...
  12. )

Note that the above example, the onClick () callback does not return value, but some other Android event listener must return a boolean value. And event-related causes. For some of the following reasons:

· OnLongClick () - returns a Boolean value to indicate whether you have consumed this incident should not be further process it. In other words, returns true that you have dealt with this incident, and stop there; return false that you do not deal with it and / or this event should continue to be passed on to other on-click listener.

· OnKey () - returns a Boolean value to indicate whether you have consumed this incident should not be further process it. In other words, returns true that you have dealt with this incident, and stop there; return false that you do not deal with it and / or this event should continue to be passed on to other on-key listener.

· OnTouch () - returns a Boolean value to indicate whether your listener consumes the event. Important is that this event can have more than each other to follow the action. So, if it receives action events you back down to false, it shows that you have not consumption, but also for the follow-up action in this incident is not interested. So, you will not be the case in the other action is called, such as gestures or the last upward movement event occurs.

Remember that key event is always submitted to the current focus of view. View them from the top of the hierarchy began to be distributed, and then turn downward, until you reach the appropriate target. If you view (or a sub-view) currently has the focus, then you can see the event through the dispatchKeyEvent () methods of distribution. Apart from your view of intercept key events, there is an option, you can use in your activities onKeyDown () and onKeyUp () to receive all events.

NOTE: Android will be the first event handler is called, followed by the default class definition in the appropriate processor. Thus, from these things return true listener will stop the event listener to other Android event will be transmitted and blocking the view of the lack of event-handler callback function. So when you return true, make sure you want to terminate the incident.

分类:Mobile 时间:2010-03-29 人气:1130
blog comments powered by Disqus


  • Mobile phone operating system introduced 2010-03-29

    Currently on the market for mobile terminal operating system Symbian camp, mainly Symbian OS, Microsoft Windows Mobile, PalmSource's Palm OS and Linux-based operating systems. 1, Windows Mobile series of mobile phone operating system Windows Mobile f

  • Evaluating open-source operating system Ylmf OS 3.0 2010-06-19

    Ylmf OS 3.0 is built based on Ubuntu 10.04, but only from the appearance of hard to judge this is derived from the Ubuntu operating system, it might be "attributed" to its precise imitation of the XP interface. Wind in the rain forest we can see

  • Simple operating system (orange os sample). 2011-01-25

    In Yuan's "write himself operating system" tells us how to write an operating system, of course, write the operating system is a very difficult thing, so we have to slow down, take a look at how to write a show on the bare minimum the smallest.

  • Linux Learning - the first day - what is the operating system? 2011-06-30

    Chapter What is Linux What is Linux 1.1 1.1.1 Computer: calculation aids Computer must have the components: Input unit: such as a mouse, keyboard, card reader machine, and so on. Central processing unit (CPU): contains the arithmetic logic, control,

  • Java access to operating system information 2011-09-21

    import java.util.Properties; Properties props=System.getProperties(); // Get system properties set String osName = props.getProperty("os.name"); // Operating system name String osArch = props.getProperty("os.arch"); // Operating system

  • What is the callback method, coverage, overload, overload 2010-05-30

    People used to call the system and procedures for the preparation of tools for ways to help yourself, a function in Yongzuo components or plug-in but for the class you need to prepare for the system to call a number of ways, these specialized methods

  • Class def & impl, Inherit & overwriting, interface & using it, event & handler 2010-12-08

    Below codes is a simply and rough example could be used as a reference if you have a long time or never touch ABAP OO but would like to use it right now in you project. REPORT YT_OO_T01. * Interface INTERFACE lif_test. DATA: v_info TYPE c LENGTH 10.

  • Google Tablet PC operating system, a prototype map interface 2010-03-31

    Google Chromium official website of the Tablet PC operating system interface diagram U.S. technology blog site TechCrunch that, Chrome OS Tablet PC operating system interface map from Google Chrome seems to be the designer Glen Murphy (Glen Murphy) h

  • Intel SAS RAID installation of CentOS 5.4 Linux operating system, the method 2010-03-30

    Motherboard: Intel S5000SAV (sas) Display technology: Inte; l Embedded Server RAID Technology II HDD: SAS hard drives configured as RAID1 array The first step: Download the latest Intel Embedded Server RAID Technology II RAID Driver Package Technical

  • Linux operating system software installation method Daquan 2010-06-18

    1, rpm package Installation steps: 1, find the appropriate package, such as soft.version.rpm, downloaded to the machine to a directory; 2, open a terminal, su - as root user; 3, cd soft.version.rpm is located; 4, type rpm-ivh soft.version.rpm Second,

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.096 (s). 12 q(s)