Android user interface-input events)

Source: Internet
Author: User

On the Android system, there are multiple ways to intercept events between users and applications. When considering user interface events, the method is to intercept events from the specific view object that interacts with the user. The View class provides this capability.

In the various view classes used to form the layout, You can note several common callback methods for UI events. The android Framework calls these methods when actions on the respective objects occur. For example, when a view object (such as a button) is touched, The ontouchevent () method of the object is called. However, to intercept this event, you must inherit the View class and override this method. However, it is impractical to extend each view object to handle such events. This is why the View class also contains a set of nested interfaces that make it easier for you to define callback methods. These interfaces called event listeners are the portals used to capture user interaction with the UI.

In most cases, you use Event Listeners to listen for interactions with users. However, sometimes you need to expand a View class to create a custom component. You may want to extend the button class to make it more refined. In this case, you must be able to use the event processor to define the default event behavior for your class.

Event listener

An event listener is an interface in the View class that contains a single callback method. When the registered listener of this view is triggered by the interaction between the user and the corresponding project in the UI, the android Framework calls these methods.

The event listener Interface contains the following callback methods:

Onclick ()

Source: view. onclicklistener interface. This method is called when you touch the project (in touch mode) or place the focus on the project.
Onlongclick ()

From the view. onlongclicklistener interface, this method is called when you touch the project for a long time (in touch mode) or press the project for a long time.

Onkey ()

This method is derived from the view. onkeylisten interface. When you place the focus on the project and press or release a key on the device, this method is called.

Ontouch ()

Derived from the view. ontouchlistener interface, this method is called when a user executes an action that matches a touch screen event (including a push action, a release action, or any gesture on the screen.

Oncreatecontextmenu ()

Derived from the viewoncreatecontextmenulistener interface, this method is called when a context menu is created.

These methods are the only member of each interface. To define these methods and use them to handle events, implement these nested interfaces in the activity or define them as an anonymous class. Then, pass your implemented instance to the respective view. Set... Listener () method. (For example, call the setonclicklistener () method and pass the onclicklistener () implementation to it ).

The following example shows how to register an on-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 also find it easier to implement the onclicklistener package as part of the activity. This will avoid loading external classes and allocating object space. 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
}
...
}

We noticed that the onclick () callback method in the code above has no return value, but some other event listener methods must return a Boolean value. The cause depends on the specific event. The reasons are as follows:

1. onlongclick () --- this method returns a Boolean value to indicate whether further action should be performed because you have used this event. That is to say, if true is returned, it indicates that you have handled this event and the event should be stopped. If false is returned, it indicates that this event has not been processed, therefore, this event should be passed to any other on-click listener.

2. onkey () --- this method returns a Boolean value to indicate whether further action should be performed because you have used this event. That is to say, if true is returned, it indicates that you have handled this event and the event should be stopped. If false is returned, it indicates that you have not handled this event, this event should be passed to any other on-key listener.

3. ontouch () --- this method returns a Boolean value to indicate whether your listener uses this event. It is important that this event can have multiple actions that follow each other. Therefore, if false is returned when you receive the push Action event, it indicates that you do not use this event and are not interested in subsequent actions from this event. Therefore, you will not call any other actions in this event, such as the final action of a gesture or event.

Remember that a key event is always the focus object sent to the current view. They start scheduling from the top layer of the view hierarchy tree, and then go down until they reach the appropriate target. If the current view (or the child view of the View) has a focus, you can see the execution track of the event through the dispatchkeyevent () method. You can select to capture the key events of the view object, because you can accept the onkeydown () and onkeyup () Events in the activity.

Note:Android calls the event processor first, and then calls the appropriate default processor in the class definition. For example, if true is returned from these methods, the event listener stops spreading the event to other Event Listeners and blocks the callback of the default event processor in the view. Therefore, true is returned only when you are sure to terminate event propagation.

Event Processor

If you are building a custom component from the View class, you need to define several default callback methods as the default event processor. In the "custom components" topic, you will learn some common callback methods for event processing. They are:

1. onkeydow (INT, keyevent) --- this method is called when a key is pressed;

2. onkeyup (INT, keyevent) --- this method is called when a pressed key pops up;

3. ontrackballevent (motionevent) --- this method is called when the trackball is rolling;

4. ontouchevent (motionevent) --- this method is called when a touch screen action occurs;

5. onfoucuschanged (Boolean, Int, rect) --- this method is called when a view object gets or loses focus.

There are some other methods you should know. They are not part of the View class, but they can directly affect the way you handle events. Therefore, when managing more complex events in the layout, consider these methods:

1. activity. dispatchtouchevent (motionevent) --- This method allows you to intercept all touch screen events before the touch screen events are distributed to the corresponding windows.

2. viewgroup. onintercepttouchevent (motionevent) --- This method allows a viewgroup object to view the events of the Child view objects distributed to it.

3. viewparent. requestdisallowintercepttouchevent (Boolean) --- Call this method on the parent view object to specify whether the parent view object should intercepttouchevent (motionevent) Touch Screen events.

Touch Screen mode

When you use a direction key or scroll wheel to browse a user interface, you need to give a focus (such as a button) to the operable project so that you can see that the interface is receiving input. However, if the device has the ability to touch and the user interacts with the interface through touch, you do not need to highlight the project or focus on a special view object. In this way, there is an interactive mode called "touch mode.

For a device with the touch capability, once the user touches the screen, the device enters the touch mode. From this point on, only the view object whose return value of the isfocusableintouchmode () method is true can be focused, such as the text editing component. Other view objects are touchable, such as buttons, which do not require focus when being touched. They simply trigger the on-click listener when being pressed.

At any time, if you click a direction key or scroll the scroll wheel, the device will exit the touch mode and find a view object that requires focus. Now, the user can restore the mode of interaction with the user interface without the touch screen.

The Touch Screen mode is maintained by the entire system (all windows and activities ). You can call isintouchmode () to check whether the device is in touch screen mode.

Processing focus

The android Framework processes the Focus activity to respond to user input. This includes changes to the focus when the view object is deleted or hidden, or when a new view object becomes valid. View objects use the isfocusable () method to indicate whether they are willing to obtain the focus. Call setfocusable () to change the current focus object. In touch mode, you can use the isfocusableintouchmode () method to query whether a view object allows focus. You can use the setfocusableintouchmode () method to change this setting.

The Focus movement is based on an algorithm that finds the most recent view objects with acceptable focus in a specific direction. In rare cases, this algorithm does not match the developer's expected behavior, in this case, you can use the XML attributes in the following layout files to explicitly overwrite the default algorithms: nextfocusdown, nextfocusleft, nextfocusright, and nextfocusup. Add one of these attributes to the view object that leaves focus. In this attribute, specify the ID of the Focus object to be moved to the next view object, 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>

In this vertical layout, browsing from the top of the first button will not go anywhere, and browsing from the bottom of the second button will not go anywhere. Now, the top button defines the bottom button as the next focal point winner (and vice versa), so that the browsing focus will switch back and forth between the two buttons.

If you like to declare a view object in the UI as a focused object (traditionally, this is not the case), you need to add the XML data of Android: focusable to the corresponding view object, in the layout declaration, set the property value to true. In Touch Screen mode, you can use the Android: focusableintouchmode attribute to set a view object as a focused object.

Call the requestfocus () method to set the focus for a specific view object.

Use the onfocuschange () method to listen to focus events (a notification is sent when a view object accepts or loses focus ).

Related Article

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.