Android event processing mechanism and finger slide examples

Source: Internet
Author: User

There are two types of event processing mechanisms on the Android platform.

Event Processing Based on callback mechanism: On the Android platform, each View has its own callback Method for event processing. developers can rewrite these callback methods in the View to implement the required response events.

Listener interface-based event processing: This is the mainstream Processing Method in the object-oriented design. It uses the delegate event processing method to delegate events to registered event listeners for processing.

 


1. Callback-based event processing: rewrite some specific callback methods of android Components

OnKeyDown ()/onKeyUp (), which is the interface KeyEvent. abstract method in Callback. All views implement this interface and override this method. This method is used to capture the events when the mobile phone keyboard is pressed. Public boolean onKeyDown (int keyCode, KeyEvent event)

OnTouchEvent (), the definition of this method in the View class, and all the View subclass override this method, the application can use this method to process the touch event of the mobile phone screen.

Public booleanonTouchEvent (MotionEvent event)

OnFocusChanged (), which can only be rewritten in View. This method is a focus change callback method. When a control overwrites this method, when the focus changes, this method is automatically called to handle the focus change events. Protected void onFocusChanged (boolean gainFocus, int direction, Rect previouslyFocusedRect)

 


2. listener-based event processing: bind a specific event listener to the android component. 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 (): Contains 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 (): Contains 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 (): Contains View. OnKeyListener. It is called when you focus on this item and press or release a key on the device.

OnTouch (): Contains 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 (): Contains View. OnCreateContextMenuListener. It is called when a context menu is being created (as the result of a continuous "Long click" action ).

 


Rules for passing and processing events on the Android system interface

If an event listener is set for the interface control, the event is first passed to the event listener.

If no event listener is set for the interface control, the interface event is directly transmitted to other event processing functions of the interface control.

Even if an event listener is set for the interface control, interface events can be passed to other event processing functions again.

Whether to pass the event to another handler is determined by the return value of the event listener handler function.

If the returned value of the listener processing function is true, it indicates that the event has been processed and no other processing functions are required to participate in the processing process, so that the event will not be passed.

If the returned value of the listener processing function is false, it indicates that the event has not completed the processing process, or other processing functions need to capture the event. The event will be passed to other event processing functions.

 


Event processing example based on listener Interface

On smartphones, many applications need coordinates and user operations when the user's fingers are operated. In view of Android development, slide is often used, so the following is an example of sliding:

:




 

<Span style = "font-size: 16px;"> activity code:
Package com. TouchView;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. MotionEvent;
Import android. view. View;
Import android. widget. TextView;

Public class TouchViewActivity extends Activity {

Private TextView eventlable;
Private TextView histroy;
Private TextView TouchView;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TouchView = (TextView) findViewById (R. id. touch_area );
Histroy = (TextView) findViewById (R. id. history_label );
Eventlable = (TextView) findViewById (R. id. event_label );

TouchView. setOnTouchListener (new View. OnTouchListener (){

@ Override
Public boolean onTouch (View v, MotionEvent event ){
Int action = event. getAction ();
Switch (action ){
// When pressing
Case (MotionEvent. ACTION_DOWN ):
Display ("ACTION_DOWN", event );
Break;
// When pressing
Case (MotionEvent. ACTION_UP ):
Int historysize = ProcessHistory (event );
Histroy. setText ("historical data" + historysize );
Display ("ACTION_UP", event );
Break;
// When you touch
Case (MotionEvent. ACTION_MOVE ):
Display ("ACTION_MOVE", event );
}
Return true;
}
});
}

Public void Display (String eventType, MotionEvent event ){
// Contact relative coordinate information
Int x = (int) event. getX ();
Int y = (int) event. getY ();
// Indicates the touch screen pressure.
Float pressure = event. getPressure ();
// Indicates the contact size
Float size = event. getSize ();
// Obtain the absolute coordinate information
Int RawX = (int) event. getRawX ();
Int RawY = (int) event. getRawY ();

String msg = "";

Msg + = "Event Type" + eventType + "\ n ";
Msg + = "relative coordinates" + String. valueOf (x) + "," + String. valueOf (y) + "\ n ";
Msg + = "absolute coordinate" + String. valueOf (RawX) + "," + String. valueOf (RawY)
+ "\ N ";
Msg + = "Contact pressure" + String. valueOf (pressure) + ",";
Msg + = "contact size" + String. valueOf (size) + "\ n ";
Eventlable. setText (msg );
}

Public int ProcessHistory (MotionEvent event ){
Int history = event. getHistorySize ();
For (int I = 0; I Long time = event. getHistoricalEventTime (I );
Float pressure = event. getHistoricalPressure (I );
Float x = event. getHistoricalX (I );
Float y = event. getHistoricalY (I );
Float size = event. getHistoricalSize (I );
}

Return history;

}

}
</Span>
<Span style = "font-size: 16px;"> activity code:
Package com. TouchView;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. MotionEvent;
Import android. view. View;
Import android. widget. TextView;
 
Public class TouchViewActivity extends Activity {
 
Private TextView eventlable;
Private TextView histroy;
Private TextView TouchView;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TouchView = (TextView) findViewById (R. id. touch_area );
Histroy = (TextView) findViewById (R. id. history_label );
Eventlable = (TextView) findViewById (R. id. event_label );
 
TouchView. setOnTouchListener (new View. OnTouchListener (){
 
@ Override
Public boolean onTouch (View v, MotionEvent event ){
Int action = event. getAction ();
Switch (action ){
// When pressing
Case (MotionEvent. ACTION_DOWN ):
Display ("ACTION_DOWN", event );
Break;
// When pressing
Case (MotionEvent. ACTION_UP ):
Int historysize = ProcessHistory (event );
Histroy. setText ("historical data" + historysize );
Display ("ACTION_UP", event );
Break;
// When you touch
Case (MotionEvent. ACTION_MOVE ):
Display ("ACTION_MOVE", event );
}
Return true;
}
});
}
 
Public void Display (String eventType, MotionEvent event ){
// Contact relative coordinate information
Int x = (int) event. getX ();
Int y = (int) event. getY ();
// Indicates the touch screen pressure.
Float pressure = event. getPressure ();
// Indicates the contact size
Float size = event. getSize ();
// Obtain the absolute coordinate information
Int RawX = (int) event. getRawX ();
Int RawY = (int) event. getRawY ();
 
String msg = "";
 
Msg + = "Event Type" + eventType + "\ n ";
Msg + = "relative coordinates" + String. valueOf (x) + "," + String. valueOf (y) + "\ n ";
Msg + = "absolute coordinate" + String. valueOf (RawX) + "," + String. valueOf (RawY)
+ "\ N ";
Msg + = "Contact pressure" + String. valueOf (pressure) + ",";
Msg + = "contact size" + String. valueOf (size) + "\ n ";
Eventlable. setText (msg );
}
 
Public int ProcessHistory (MotionEvent event ){
Int history = event. getHistorySize ();
For (int I = 0; I Long time = event. getHistoricalEventTime (I );
Float pressure = event. getHistoricalPressure (I );
Float x = event. getHistoricalX (I );
Float y = event. getHistoricalY (I );
Float size = event. getHistoricalSize (I );
}
 
Return history;
 
}
 
}
</Span>


Main. xml code:

 

 


[Html] <span style = "font-size: 16px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView
Android: id = "@ + id/touch_area"
Android: layout_width = "fill_parent"
Android: layout_height = "300dip"
Android: background = "# 0FF"
Android: textColor = "# FFFFFF"
Android: text = "Touch event test area"
/>
<TextView
Android: id = "@ + id/history_label"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "historical data"
/>
<TextView
Android: id = "@ + id/event_label"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Touch event :"
/>
</LinearLayout>
</Span>

 

From Peking University-Google Android lab
 

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.