Android UI event processing

Source: Internet
Author: User

On the Android platform, there are many ways to capture user trigger events on the interface. The View class provides these methods. When you use various view views to layout the interface, you will find several common callback methods to capture useful UI trigger events. When an event is triggered on a view object, these methods are called by the system framework through this object. For example, when a view (such as a button) is clicked, The ontouchevent () method is called on this object, to capture and process events, you must inherit a class and reload these methods so that you can define specific processing logic. Obviously, it is easier to understand, why are you nested interface classes with these callback methods when using the View class? These interfaces are called event listeners. They are tools for you to obtain UI Interaction Events and inherit the View class, in order to create a custom group, you may want to inherit the button, and you will be more likely to use event listening to capture user interaction. In some cases, you can use Event Handlers of the class. to predefine the processing method of the event.

Event Listeners

The event listener in the View class is an interface with a callback method. When the component in the UI is triggered by the user, these methods will be called by the system framework.

Onclick ()

From view. onclicklistener, it will be called when you click this item (in touch mode), or when the cursor gathers on this item, press the "OK" Key, navigation key, or trackball.

Onlongclick ()

From view. onlongclicklistener. It will be called when the long-pressed item (in touch mode), or when the cursor is gathered on this item, press the "OK" Key, navigation key, or trackball.

Onfocuschange ()

From view. onfocuschangelistener it will be called when the cursor moves or leaves this item,

Onkey ()

From view. onkeylistener... it will be called, when the cursor moves to this item, when you press and release a button

Ontouch ()

From view. ontouchlistener. It will be called.

Oncreatecontextmenu ()

From view. oncreatecontextmenulistener. It will be called. When the context menu is created (due to the continuous "Long press"), see the creating menus discussion for more information.
These methods correspond to nested interface classes one by one. If you are sure that one of the methods is used to process your interactive events, you need to implement the interface with this method in the activity, and use it as an anonymous class, and then, through the instance view. set... listener () method to set the listener (for example, call setonclicklistener () to set onclicklistener as the listener)

The following is an example of setting a 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);     ... }

}
We will find this column belowUsing activity to implement the onclicklistener interface is more convenient as part of it, without loading additional classes and objects.

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 above example shows that onclick () does not return a value, but some event processing methods must include a return value. It depends on the specific event and some causes for this, see the following example:
Onlongclick ()
The Boolean value returned by it indicates that you have processed the event, or you should continue to upload it. If the return value is true, the processing is completed and the transfer is stopped. If the return value is false, the event is not completed, or the event must be passed to another listener.
Onkey ()
The Boolean value returned by it indicates that you have processed the event, or you should continue to upload it. If the return value is true, the processing is completed and the transfer is stopped. If the return value is false, the event is not completed, or the event must be passed to another listener.
Ontouch ()
It returns a Boolean value indicating whether you have completed the action of this event. What is important is that there may be many subsequent actions. In this way, if you return false, it indicates that in the next follow-up action, you have not completed the previous action and are not willing to handle the subsequent action. Therefore, the subsequent action of this event will not be called. Such as fingure gestures or final action events
Remember: the event we are concerned with must be the focus of the highlighted aggregation, which is passed down from the overall view (top-level) to the components we want to focus on, when the focus is concentrated in this view (or a subview in the view), you can use dispatchkeyevent () as an alternative method to capture key events on The View, you can also use onkeydown () and onkeyup (). to capture interaction activities in all events

Note: In the android framework, event handlers is called to process the event first and then pass it to the predefined level-2 handlers appropriately. Therefore, if true is returned, the event will be stopped, and the callback of the default event processing method in the view will also be blocked. Therefore, if you return true, it indicates that you want to terminate the event. (This is a bit difficult to understand... Original article: Android will call Event Handlers first and then the appropriate default handlers from the class definition second. As such, returningTrueFrom these Event Listeners will stop the propagation of the event to other Event Listeners and will also block the callback to the default event handler in the view. so be certain that you want to terminate the event when you returnTrue.)

Event Handlers

If you create a custom component that inherits the view, you can define some callback methods as the default event handler. For building M components in this file, you will learn some common callback methods for event processing, including:

  • onKeyDown(int, KeyEvent)-Called when a new key event occurs.
  • onKeyUp(int, KeyEvent)-Called when a key up event occurs.
  • onTrackballEvent(MotionEvent)-Called when a trackball motion event occurs.
  • onTouchEvent(MotionEvent)-Called when a touch screen motion event occurs.
  • onFocusChanged(boolean, int, Rect)-Called when the view gains or loses focus.

    There are other methods that do not belong to the View class, but can directly affect the way you handle events. Therefore, you can consider these methods to manage more complex events in the layout:

  • Activity.dispatchTouchEvent(MotionEvent)-This allows yourActivityTo intercept all touch events before they are dispatched to the window.
  • ViewGroup.onInterceptTouchEvent(MotionEvent)-This allowsViewGroupTo watch events as they are dispatched to child views.
  • ViewParent.requestDisallowInterceptTouchEvent(boolean)-Call this upon a parent view to indicate that it shoshould not intercept touch eventsonInterceptTouchEvent(MotionEvent).

     

    Touch Mode

    When a user uses a direction key or trackball to browse the user interface, it is necessary to give a focus on an operable component (such as a button) so that the user can see that it will accept the input command. If the device has the touch function, when the user interacts with the interface, there is no need for a highlighted component or a focus on The View. Therefore, the interaction name of the mode is "touch mode ". For a touch device, once a user contacts the screen, the device enters the touch mode. After a user clicks a view, only the method is available.Isfocusableintouchmode() Only when the returned result is true will there be a concentrated focus, such as a text editing tool. Other interfaces can only be touched, but do not focus (highlighted). If a button is clicked, it will not focus, when it is pressed, only the callback method of the On-click listener is called.
    When a user is in touch with a direction key or a rolling track ball, the device will exit the touch mode and focus will be gathered. The user can resume interaction with the keyboard on the user interface without having to be on the screen. The status of the touch mode is maintained by the entire system (all windows and activities). to query the current status, you can callIsintouchmode() Method to obtain and see if the device is currently in touch mode.

    Handling focus

    The system framework will process the Daily Focus movement to respond to user input. It scratches and changes the focus (when the interface is removed, hidden, or changed to available as a new view ),Isfocusable() In this method, we can know whether the view is qualified to accept the focus, or passSetfocusable(). To set the view to accept the focus, corresponding to the touch mode, you can callIsfocusableintouchmode(). To determine whether there is a focus to respond to point touch, you can also useSetfocusableintouchmode(). To set whether there is a focus to respond to the touch.

    The algorithm for controlling the focus of the system framework to move to another component is the component that is adjacent to one party. In a few cases, the default algorithm may not meet the developer's expectation. In this case, you can override the layout file of the following XML attributes:Nextfocusdown, nextfocusleft, nextfocusright, and nextfocusupSet their values to be clear
    Focus moves the ID of the next interface from the current interface. 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 general, in this vertical layout, the browsing focus starts from the first button, not from the second or other. Now topbuttont has determined the bottom through nextfocusup (and vice versa.

    Generally, if you want to declare that the user interface has the focus qualification (if this interface is not traditionally available), you can add it in the XML layout.Android: focusableAnd set its value. You can also declare that you are qualified to focus in touch mode, and add onlyAndroid: focusableintouchmodeAnd set its value. When a user request aggregates focus on a page, it will callRequestfocus(). This method. Listens to the Focus activity (will be notified if the focus is obtained or if the focus is lost) and callsOnfocuschange(). This method is also the Event Listeners discussed in the previous section.

    Summary:
    1. For processing UI control events, you need to set the corresponding listener and implement the corresponding event processing program. There are two implementation methods: one is to define an onclicklistener instance and use setonclicklistener to bind the listener; the other isUse activity to implement the onclicklistener interface and use it as a part of it, which is more convenient and saves time for loading additional classes and objects.
    2. For mobile phones that support touch screens, you can set the UI in touch mode.Isintouchmode() To obtain the status of the touch mode.
    3. Another focus of UI processing is the focus setting and switching. Focus settings: PassSetfocusableOrSetfocusableintouchmodeSet acceptable focus.IsfocusableOrIsfocusableintouchmode to determine whether the focus is acceptable. Focus switching: Write nextfocusdown and other attribute settings for the XML layout file.

    Reference: http://developer.android.com/guide/topics/ui/ui-events.html

    Eoeandroid community group

  • 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.