Presentation is loading. Please wait.

Presentation is loading. Please wait.

Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–

Similar presentations


Presentation on theme: "Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–"— Presentation transcript:

1 Events

2 Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked– key typed –menu item selected– etc. The general approach is to capture events from the specific view object that the user interacts with.

3 Slide 3©SoftMoore Consulting Semantic versus Low-Level Events There is a distinction between low-level events and semantic or high-level events. A semantic event is a high-level encapsulation of what the user is doing. It usually encapsulates several low- level events. Example: Clicking a button is a semantic event. Low-level events include the left mouse button being pressed and released while “inside” the button area on the screen. Whenever possible, listen for semantic events.

4 Slide 4©SoftMoore Consulting Event Sources and Listeners Events are transmitted from event sources (e.g., buttons or scrollbars) to event listeners. An event source is an object that knows when/how the event occurs. Event sources report on events. An event listener is an object that needs to be notified when a certain event occurs in order to perform some action in response to the event.

5 Event Listeners An event listener is an interface nested within in the View class that contains a single callback method for handling a specific event. Example: Interface View.OnClickListener contains a single method, onClick(), that is called by the Android framework when the user clicks on the view object. Summary of Event Handling in Android –Create a class that implements the callback interface for that event; e.g., a class that handles button clicks. –Call a corresponding “ set ” method to register that class with the UI component to handle the event. Slide 5©SoftMoore Consulting

6 Example: Handling a Button Click (Preferred Approach) Button myButton = (Button) findViewById(R.id.myButton); myButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {... // actions when the button is clicked } }); Slide 6©SoftMoore Consulting This approach creates an instance of an anonymous inner class to implement the onClick() method. With Java 8, you should also be able to use a lambda expression, but lambda expressions are not yet supported by Android.

7 setOnClickListener() Sequence Diagram for the Example Slide 7©SoftMoore Consulting myButton : Button: OnClickListener user clicks the button onClick() : Activity «create» findViewById() event source event listener

8 Example: Handling a Button Click (A Second Alternative) public class MyActivity extends Activity implements View.OnClickListener { protected void onCreate(Bundle savedValues) {... Button button = (Button)findViewById(R.id.corky); button.setOnClickListener(this); } public void onClick(View v) {... // actions when the button is clicked }... } Slide 8©SoftMoore Consulting

9 Example: Handling a Button Click (A Second Alternative − continued) Limitation: Since there is only one onClick() method, MyActivity can respond in only one way to a click event. The onClick() method must contain a multiway branch (e.g., switch statement or nested if/else if statements) in order to handle clicks on different buttons. Slide 9©SoftMoore Consulting

10 Example: Handling a Button Click (A Third Alternative) // separate class (could be in same file as the Activity class) class MyOnClickListener implements View.OnClickListener { public void onClick(View v) {... // actions when the button is clicked } } // in the Activity class protected void onCreate(Bundle savedValues) {... Button button = (Button)findViewById(R.id.corky); button.setOnClickListener(new MyOnClickListener()); } Slide 10©SoftMoore Consulting

11 Common Event Listener Callback Methods onClick() –callback interface: View.OnClickListener –called by touching the item or by pressing enter when the item has focus onLongClick() –callback interface: View.OnLongClickListener –called when the user touches and holds the item onTouch() –callback interface: View.OnTouchListener –called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen within the bounds of the item. Slide 11©SoftMoore Consulting

12 Consuming Events Events are dispatched starting at the top object in the view hierarchy and then down the hierarchy until they reach the appropriate destination. The onClick() method does not return a value, but several callback methods return a boolean value to indicate whether or not the event has been “consumed”. A callback method returns true to indicate that the event has been handled (consumed) and that propagation of the event should stop at that point. Returning false indicates that the event has not been handled and that propagation should continue to other listeners in the hierarchy. Slide 12©SoftMoore Consulting

13 Relevant Links Input Events http://developer.android.com/guide/topics/ui/ui-events.html Buttons http://developer.android.com/guide/topics/ui/controls/button.html Slide 13©SoftMoore Consulting


Download ppt "Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–"

Similar presentations


Ads by Google