Android Dev guide series 13: handling UI events on the user interface
Lazy bones (http://blog.csdn.com/iamlazybone)
Processing of UI events
In user interaction, there are more than one method to intercept events. The View class provides some special methods to capture events from a special view object that interacts with the user.
In various view classes, you need to write your own layout. You can declare some common callback methods to respond to UI events. When the corresponding action occurs, these methods are automatically called by the android framework. For example, when a view is touched, The ontouchevent () method is called. However, to intercept these methods, we need to rewrite them. Of course, rewriting every method is obviously not practical, which is why the View class has some necessary interfaces so that you can use them more easily. These interfaces are called event listeners, which is the key to capturing user operations on the interface.
When you often use Event Listeners for user interaction, you sometimes need to write a class that inherits the view and customize a part. If you want to inherit a button class to implement more functions, you can use the Event Handlers class to define the default event processing class.
Event listener
The event listener is an interface of the View class that contains a simple callback method. When a user triggers a registered event, the android system framework automatically calls the corresponding method.
The event listener includes the following methods:
Onclick ()
From view. onclicklistener. When the view is touched or the focus is on the view, the user presses the confirm key or navigation key.
Onlongclick ()
From view. onlongclicklistener. When the view is touched for more than 1 second, or the focus is on the view, the method is called when the user presses the OK or navigation key for more than 1 second.
Onfocyschange ()
From view. onfocuschangelistener. Called when focus leaves view.
Onkey ()
From view. onkeylistener. The method is called when a key is pressed or popped up.
Ontouch ()
From view. ontouchlistener. It is called when a user operates the screen, including pressing release or other operations.
Oncreatecontextmenu ()
From view. oncreatecontextmenulistener. Called when a context menu is created.
These methods all have their own interface methods. If you want to define these methods to handle your own events, you only need to use anonymous classes in the activity. Use the view. setxxxlistener () method to pass the Anonymous class to the view.
The following example demonstrates how to register a listener for a button:
// Create an anonymous Implementation of onclicklistener <br/> private onclicklistener mcorkylistener = new onclicklistener () {<br/> Public void onclick (view V) {<br/> // do something when the button is clicked <br/>}< br/>}; <br/> protected void oncreate (bundle savedvalues) {<br/>... <br/> // capture our button from layout <br/> button = (button) findviewbyid (R. id. corky); <br/> // Register The onclick listener with the Implementation above <br/> button. setonclicklistener (mcorkylistener); <br/>... <br/>}
You will find that it is very convenient for the activity to implement the listener interface, and it will allow your program to avoid unnecessary object allocation.
Public class exampleactivity extends activity implements onclicklistener {<br/> protected void oncreate (bundle savedvalues) {<br/>... <br/> button = (button) findviewbyid (R. id. corky); <br/> button. setonclicklistener (this); <br/>}< br/> // implement the onclicklistener callback <br/> Public void onclick (view V) {<br/> // do something when the button is clicked <br/>}< br/>... <br/>}
Note that the onclick () callback method in the preceding example does not return a value, but other time listeners must return a Boolean value, which is determined by the listener type. For example:
Onlongclick ()
This method returns a Boolean value indicating that the event is in progress. That is, if true is returned, the event is being processed and will not be passed.
Onkey ()
A boolean value will also be returned. If true is returned, this button event will be processed and will not continue to respond. If you do not process the event or want the event to be responded by another listener, false is returned.
Ontouch ()
Returns the same as above. If true is returned, it will not be passed. The focus is that there are many types of events related to this event. For example, if you return false when a key-pressed event is received, it indicates that you will not process this event. Therefore, you will not handle this event, such as gestures or other subsequent events.
Remember that these key events will always make the current Veiw the current focus, and they will be passed at the first level from the top of the view hierarchy until they reach where they should be responded. If your view or subview has a focus, you can see how the event is passed to the dispatchkeyevent () method. As an alternative to capturing key events, you can receive all onkeydown () and onkeyup () Events in the activity.
Note: Android first calls the event handler and then calls the Custom Event Handler class. For example, returning true will prevent the event from being passed to the next layer and prevent the system from doing some default processing. Therefore, if you are sure to terminate the event, true will be returned.
Event Processor
If you have created a custom view component, You need to define several callback methods. In the building M components document, you will learn the following callback events:
Onkeydown (INT, ketevent); the key is pressed
Onkeyup (INT, keyevent );
Ontrackballevent (motionevent); trackball event
Ontouchevent (motionevent); touch screen event
Onfocuschanged (Boolean, Int, rect); Focus change event
Here are some methods you need to know. They are not part of the View class, but will directly affect your processing of the event. When encountering a very complex layout, consider the following methods:
Activity. dispatchtouchevent (motionevent)
It allows your activity to intercept all touch events.
Viewgroup. onintercepttouchevent (motionevent)
Allow veiwgroup to observe subview events
Viewparent. requestdisallowintercepttouchevent (Boolean)
Tell the parent view not to use relevant methods to intercept events.
Touch Mode
When a user is using a navigation key or trackball to interact with a mobile phone, it is necessary to give a focus to the currently interactive item, such as the button, so that the user can see the received input, or the current control. If a device supports touch, you do not need to use the above highlighted method to tell the user. Therefore, this touch mode is available. For devices with the touch function, once the user touches the screen, the device enters the touch mode. Only those controls whose isfocusableintouchmode () returns true can obtain the focus, such as the text edit box. Other views are touchable, such as buttons, but the focus is not displayed when clicked. When pressed, their event listeners will be invalid.
When you click the navigation key or trackball at any time, the device will exit touch mode and find a view to display the focus. Now, you do not need to touch the screen to interact with the device.
Touch mode exists in the entire system. Including window and activity. You can use the isintouchmode () method to query the current mode.
Focus Processing
The Framework processes regular focus change events to respond to user input. Including the display and hiding of some veiws. View uses the isfocusable () method to set whether the focus can be obtained. You can call setfocusable () to change the focusable state. When the view is in touch mode, you can call the isfocusableintouchmode () method to check whether it is supported and set it through setfocusableintouchmode.
The Focus movement is determined based on a view closest to a given direction. In rare cases, the default algorithm may not meet developers' expectations. In this case, you can use XML to clearly determine the transfer of focus. For example:
<Linearlayout <br/> Android: Orientation = "vertical" <br/>...> <br/> <button Android: Id = "@ + ID/top" <br/> Android: nextfocusup = "@ + ID/bottom" <br/>... /> <br/> <button Android: Id = "@ + ID/bottom" <br/> Android: nextfocusdown = "@ + ID/top" <br/>... /> <br/> </linearlayout>
Generally, in a vertical layout, moving the trackball or navigation key down or up cannot move the focus. Now, you can use the definition of XML to move the focus.
If you want to declare a view that can get the focus, you can add the Android: focusable attribute in XML and set it to true. You can also add Android: focusableintouchmode in touch mode.
The requestfocus () method can be used to set the focus of a specific mentoring.
Use the onfocuschange () method to listen for focus events.