[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.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of [Change] Android event listener callback methods of

  • hibernate Technical Study Notes (first)

    Introduction: Model does not match (impedance mismatch) java object-oriented language, object model, its key concepts are: inheritance, association, polymorphism, etc.; database is the relational model, its key concepts are: tables, primary keys, for ...

  • hibernate study of the second

    Persistence of three main points: 1, a statement for persistent fields accessors (accessors) and whether the variable signs (mutators) Property statement is not necessarily required for the public's. Hibernate can be default, protected or private ...

  • Rails2.0.2 change the default DB adpter

    In Rails2.0.2 rails demo ... ... MissingSourceFile in SayController # hello no such file to load - sqlite3 RAILS_ROOT: / home / kenb / rails-projects / demo ... ... Checked config / database.yml, adpter default is set become the sqlite3. Check the ra ...

  • ROR resources

    Ruby Web site resources: ruby official website: http://www.ruby-lang.org ruby doc official website: http://www.ruby-doc.org rubyonrails official website: http://www.rubyonrails.org programming ruby online edition (Ruby studying the "Bible") ...

  • Hibernate Inteceptor

    The end of the project stage, the client suddenly put forward a very troublesome but normal demand, the system records all changes must be carried out. Formats such as: 2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3 The firs ...

  • Some interview questions java

    The first is the company give you a chance to meet, it is necessary to know to meet from time to equal the interview, and have a lot of companies to see you at the first time will give you a ready point of doing something trivial, these questions, althoug

  • hibernate using c3p0 connection pooling

    Private http://www.lifevv.com/tenyo/doc/20070605102040991.html c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is v ...

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • In the servlet use Bean

    According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries