Android Development Guide-User Interface-Event Processing

Source: Internet
Author: User
Tags xml attribute

Handle user interface Events Handling UI events

On Android, there is more than one way to listen for events that interact between users and applications. For events on the user interface, the method of listening is to intercept these events from a specific view object that interacts with the user. View class provides corresponding means.

In various view classes used to build the layout, you may notice that some common callback methods seem useful for user interface events. These methods are called by the android framework when related actions of the object occur. For example, when a view (such as a button) is touched, The ontouchevent () method on the object is called. However, to listen for this event, you must extend this class and override this method. Obviously, extending each view object you want to use (only processing an event) is absurd. This is why the View class also contains a set of nested interfaces, which contain callback functions that are much simpler to implement. These interfaces are called event listeners.
Listeners is a "ticket" used to intercept user interaction with your interface ".

When you use Event Listeners more commonly to listen for user actions, you may always have to expand a View class to create a custom component. Maybe you want to extend the button class to make some things more fancy. In this case, you will be able to make the event processor event
The handlers class defines the default event behavior for your class.

 

Event Listeners

The event listener is an interface of the View class and contains a separate callback method. These methods are called by the android framework when the listener registered in the view is triggered by user interface operations. The following callback methods are included in the event listener interface:

Onclick ()

Included in view. onclicklistener. It is called when the user touches this item (in touch mode), or focuses on this item through a browser key or a trackball, And then presses the "OK" key or the trackball.

Onlongclick ()

Included in view. onlongclicklistener. When the user touches and controls the item (in touch mode), or focuses on the item through the Browse key or trackball, then hold the "OK" key or press the trackball (in one second) is called.

Onfocuschange ()

Included in view. onfocuschangelistener. It is called when the user enters or leaves the item by using the Browse key or tracking ball.

Onkey ()

Included in view. onkeylistener. It is called when you focus on this item and press or release a key on the device.

Ontouch ()

Included in view. ontouchlistener. A user-executed action is called as a touch event, including pressing, releasing, or any movement gesture on the screen (within the boundary of this item ).

Oncreatecontextmenu ()

Included in view. oncreatecontextmenulistener. It is called when a context menu is being created (as the result of a continuous "Long click" action ). See creating
Menus for more information.

These methods are the only "Residents" of their corresponding interfaces ". To define these methods and process your events, implement this nested interface in your activities or define it as an anonymous class. Then, pass an instance of your implementation to the respective view. Set... listener () method. (For example, call setonclicklistener () and pass it to your onclicklistener implementation .)

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

// Create an anonymous Implementation of onclicklistener

Private onclicklistener mcorkylistener = new onclicklistener (){

Public void onclick (view v ){

// Do something when the button is clicked

}

};

 

Protected void oncreate (bundle savedvalues ){

...

// Capture our button from Layout

Button button = (button) findviewbyid (R. Id. Corky );

// Register The onclick listener with the Implementation above

Button. setonclicklistener (mcorkylistener );

...

}

You may find it much easier to use onclicklistener as part of the activity. This avoids additional class loading and object allocation. For example:

Public class exampleactivity extends activity implements onclicklistener {

Protected void oncreate (bundle savedvalues ){

...

Button button = (button) findviewbyid (R. Id. Corky );

Button. setonclicklistener (this );

}

 

// Implement the onclicklistener callback

Public void onclick (view v ){

// Do something when the button is clicked

}

...

}

Note that the onclick () callback in the preceding example does not return a value, but some other event listeners must return a Boolean value. The cause is related to the event. Some of the reasons are as follows:

· Onlongclick ()-returns a Boolean value to indicate whether you have consumed the event and should not process it further. That is, returnTrueIndicates that you have handled this event and the result is returned.FalseIt indicates that you have not processed it and/or the event should be handed over to other on-click listeners.

· Onkey ()-returns a Boolean value to indicate whether you have consumed the event and should not process it further. That is, returnTrueIndicates that you have handled this event and the result is returned.FalseIt indicates that you have not processed it and/or the event should be handed over to other on-key listeners.

· Ontouch ()-returns a Boolean value to indicate whether your listener has consumed this event. It is important that this event can have multiple actions that follow each other. Therefore, if you receive a downward Action eventFalseIt indicates that you have not consumed this event and are not interested in subsequent actions. Then, you will not be called by other actions in the event, such as gestures or the last upward Action event.

Remember that key events are always submitted to the view with the current focus. They are distributed from the top layer of the view level, and then downward until they reach the appropriate target. If your view (or a subview) has a focus, you can see that the event is distributed by the dispatchkeyevent () method. In addition to intercepting key events from your view, you can also use onkeydown () and onkeyup () in your activity to receive all events.

Note:Android will first call the event processor, followed by the appropriate default processor in the class definition. In this way, from these event listenersTrueSpreading the stop event to other event listeners also blocks the callback function of the event handler in the view. Therefore, when you returnTrueConfirm that you want to terminate this event.

Event Handler

If you create a custom component from the view, you can define some callback methods to be used as the default event processor. Create custom component building m
In the components document, you will learn some common callback functions used for event processing, including:

· Onkeydown (INT, keyevent)-called when a new key event occurs.

· Onkeyup (INT, keyevent )-
Called when an up key event occurs.

· Ontrackballevent (motionevent )-
Called when a ball tracking event occurs.

· Ontouchevent (motionevent )-
It is called when a touch screen movement event occurs.

· Onfocuschanged (Boolean,
Int, rect)-called when the view gets or loses focus.

You should know that there are other methods that are not part of the View class, but can directly affect the way you handle events. Therefore, when managing more complex events in a layout, consider these methods:

· Activity. dispatchtouchevent (motionevent )-
This allows your activities to capture all touch events before being distributed to the window.

· Viewgroup. onintercepttouchevent (motionevent )-
This allows a viewgroup to observe these events when distributing them to subviews. Viewparent. requestdisallowintercepttouchevent (Boolean )-
This method is called on a parent view to indicate that it should not capture touch events through onintercepttouchevent (motionevent.

Touch Mode

When you use a direction key or a tracking ball to browse the user interface, it is necessary to set a focus for the item (such as a button) that can be operated by the user so that the user can know which item will accept the input. However, if the device has the touch function and the user interacts with the interface through touch, there is no need to highlight items or set the focus to a specific view. In this way, there is an interactive mode called "touch mode ".

For a device with the touch function, once you touch the screen, the device enters the touch mode. Since then, only an isfocusableintouchmode () view can be focused, such as a text editorial component. Other touchable views, such as buttons, do not accept focus when touched; they simply trigger the on-click listener when pressed. If you press the arrow key or scroll the tracking ball at any time, the device will exit the touch mode and find a view to accept the focus. The user may not restore interface interaction by touching the screen.

Maintenance of the touch mode status runs through the entire system (all windows and activities ). To query the current status, you can call isintouchmode () to check whether the device is in touch mode.

Processing focus handling focus

The framework will process regular focus movement based on user input. This includes changing the focus when the view is deleted or hidden or when a new view appears. Views use the isfocusable () method to indicate their willingness to obtain the focus. To change whether the view can accept the focus, call setfocusable (). In touch mode, you can use isfocusableintouchmode () to check whether a view allows focus reception. You can change it through the setfocusableintouchmode () method. Focus movement is based on an algorithm for finding nearest neighbor in a given direction. In rare cases, the default algorithm may not match the developer's will behavior. In these cases, you can provide explicit rewriting through the XML Attribute in the following layout file:Nextfocusdown,Nextfocusleft,Nextfocusright,
AndNextfocusup. Add one of these attributes to a view without focus. Define the attribute value as the ID of the view with focus. For example:

<Linearlayout

Android: Orientation = "vertical"

...>

<Button Android: Id = "@ + ID/top"

Android: nextfocusup = "@ + ID/bottom"

.../>

<Button Android: Id = "@ + ID/bottom"

Android: nextfocusdown = "@ + ID/top"

.../>

</Linearlayout>

Generally, in this vertical layout, browsing from the first button up or from the second button down will not move to other places. Now the top button has been definedNextfocusup(And vice versa), the browsing focus will move from top to bottom and from bottom to top cyclically.

If you want to declare a focused view on the user interface (usually not like this), you can add the Android: focusable XML attribute to the view in your layout definition. Set its valueTrue. You can also declare a view to be focused in touch mode through Android: focusableintouchmode.

To request a specific view that accepts the focus, call requestfocus ().

To listen for a focus event (notified when a view gets or loses focus), use onfocuschange (), such as the event listener event above.
As described in the listeners chapter.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.