In Android, there's more than one way to intercept user interaction events from your app. Consider events in your user interface by capturing events from a specified view object in the user interface. This view provides a way to do this.
In the different view classes you use to make up your layout, you may notice that some public callback methods seem to be useful for UI events. These methods are called by the Android framework when individual operations occur in the object. For example, when a view (a button) is touched, the Ontouchevent () method in this object is called. However, in order to intercept this event, you must inherit the class (button) and override the method (Ontouchevent). However, it may not be practical to inherit each view object in order to handle such an event. This is why the view class often contains a set of nested calling interfaces that allow you to follow a convenient definition (event-handling method). These interfaces, called event listeners (listeners), are used to capture the user's interaction with the UI. When you use event listeners more commonly for your users, when you want to inherit a view class, you might be able to use it once to create a custom component. Maybe you want to inheritButtonclass to make something more beautiful. In such a case, you might need to define a default event behavior for your class, using the event handler for the class handlers. Event listener an event Listeners is an interface in the view class that contains a simple callback function. These methods will be called by the Android framework when the listener of the view has been registered and triggered by the user UI entry. The following callback methods are included for the event listener interface:
onClick()
来自View.OnClickListener
.。 When the user touches the item (in touch mode), or uses the navigation key or trackball to gather focus on the item and presses the appropriate "enter" key or performs a down action on the trackball.
onLongClick()
From
View.OnLongClickListener
.。 When the user continues to touch item (in touch mode), or use a navigation key or trackball to focus on the item and continue to press the appropriate "enter" key or to perform a continuous press operation on the trackball (for one second).
onFocusChange()
From
View.OnFocusChangeListener
When the user navigates to or leaves the item using the navigation key or trackball.
onKey()
来自View.OnKeyListener
. When the user focuses on the item and presses or releases the key on the device
onTouch()
来自View.OnTouchListener
When a user performs a qualifying touch operation, including press, release, or any gesture action on the screen (within the bounds of the project)
onCreateContextMenu()
来自View.OnCreateContextMenuListener
. Called when the context menu starts to build (such as the result of persistent "long press"). View a discussion of the context menu in the Menus Development wizard. These methods are the only means of their respective interfaces. To define one of these methods and handle your event, implement a nested interface in your activity, or define it as an anonymous class. Then, pass one of your instance references to the respective view.set ... Listener () method. Method (for example, call Setonclicklistener () and pass a onclicklistener that you instantiate) The following case shows how to register a On-click listener for a button.
// 为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 can find the implementation onclicklistener more conveniently as part of the activity. This avoids additional class loading and object assignment. As follows:
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 in the example above
onClick()
The callback does not return a value, but some other event-handling methods must return a Boolean type. The reason depends on the event. There are a few reasons for this:
onLongClick()
-The method returns a Boolean type that indicates whether you have consumed the event, and it should not be passed further. That is, returning TRUE indicates that you have handled the event and that it should stop here; return FALSE if you have not handled it or the event should continue to be passed to other On-click listeners.
onKey()
-The method returns a Boolean type that indicates whether you have consumed the event and should not be passed further. That is, returning TRUE indicates that you have handled the event and that it should stop here; return FALSE if you have not handled it or the event should continue to be passed to other On-click listeners.
onTouch()
-The method returns a Boolean type to indicate whether you have consumed the event. It is important that the event can have more than one action that matches the trigger condition. Therefore, when the press event is accepted, if you return false, you are not dealing with the event and are not interested in the follow-up action of the event. Therefore, you will not invoke the event for each operation, such as gesture actions, or the last lift action (up action event).
Keep in mind that hardware button events are always passed to the current focus view. Distribute downward from the top of the view until you reach the appropriate place. If your view (or the child view in view) has the focus, then you can see the movement of the event through the Dispatchkeyevent () method. As an alternative way to capture key events from your view, you can also
onKeyDown()
And
onKeyUp()
A variety of events are received within. Also, when your app enters text, it's important to note that many devices have only software input methods. These methods do not require key support, or use sound input, handwriting, and so on. Even if an input method renders a keyboard-like interface, it usually does not trigger events such as onkeydown (). You should never create a UI to constrain the press of a specified key unless you want to limit the hardware disk that your app uses. In particular, when the user presses the Back button, do not rely on these methods to confirm the input, instead use actions like Ime_action_done to indicate the input method and how to react to your application. This may change its UI in a meaningful way. Avoid imagining how a software input method should work, and only trust it to support formatted text for your app.
Note: Android will call the event handler first, and then the appropriate default handler defined in the class is called. Similarly, returning true from these event listeners will stop the event from being propagated to other event listeners, and block callbacks for default event handling in view. Therefore, when you return true, make sure that you want to terminate the event. Event handlers if you want to customize a view component, you might define some callback methods to use as the default event handlers. In custom components, in this article, you will see some common callback functions that are used as event handlers, including:
onKeyDown(int, KeyEvent)
-Called when a new keystroke event occurs
onKeyUp(int, KeyEvent)
-Called when a key is released (key up) event occurs
onTrackballEvent(MotionEvent)
-When a trackball motion event occurs
onTouchEvent(MotionEvent)
-When a screen touch event occurs
onFocusChanged(boolean, int, Rect)
-When the view Gets or loses focus
Here are some other ways you need to be aware that they are not part of the view class, but can directly affect how you can handle events. Therefore, consider these methods when you are managing more composite events in your layout:
Activity.dispatchTouchEvent(MotionEvent)
-Allow your activity to intercept all touch events before they are distributed to Windows.
ViewGroup.onInterceptTouchEvent(MotionEvent)
-Allows a viewgroup to see if the event is dispatched to the child view.
ViewParent.requestDisallowInterceptTouchEvent(boolean)
-calling the method depends on the parent view, indicating that it should not pass onInterceptTouchEvent(MotionEvent)
. Intercept Touch events.
Touch mode when a user moves in the user interface using a direction key or trackball, it is necessary to give the active item (such as a button) focus, so that the user can see what input will be accepted. If the device has touch capability, and the user begins to interact with the interface through touch, there is no need to highlight the item or give the specified view a focus. Therefore, there is an interactive mode called "Touch Mode" (the current Android phone is mostly this mode, because it does not have a hardware input device) for a touchable device, the mobile user touches the screen and the device enters touch mode. From this point on, only when view is Isfocusableintouchmode () is true can it be focused, such as a text editing component. Other view are touchable, such as buttons, which will not get focus when touched, and when pressed, they only activate their on-click listeners. At any time, the user taps a direction key or slides the trackball, and the device exits the touch mode and finds a view to get the focus. Now, the user can continue interacting with the user interface without using the touch screen this way. The touch mode status is maintained throughout the system (all windows and Activitys). To query the current state, you can call the
isInTouchMode()
To see if the device is currently in touch mode. The process focus (handling focus) framework handles the general focus movement when it responds to user input. This includes changing the focus when the view is removed or hidden, or a new view becomes available. View uses the Isfocusable () method to indicate their willingness to gain focus. Change whether the view can get focus, call Setfocusable (). When in touch mode, you may need to query whether a view allows focus by Isfocusableintouchmode (). By using
setFocusableInTouchMode()
You can change it. The focus movement is based on an algorithm that looks for the nearest (view) in a given direction. In rare cases, the default algorithm may not meet the developer's requirements. In this case, you can provide a detailed overlay by using the following XML attribute in the layout file:nextFocusDown,nextFocusLeft,nextFocusRightAndnextFocusUp。Add one of these properties to the view. The value of this property is the ID of the next view that needs to be focused. As follows:
<LinearLayout
android:orientation="vertical"
... >
<Button android:id="@+id/top"
android:nextFocusUp="@+id/bottom"
... />
<Button android:id="@+id/bottom"
android:nextFocusDown="@+id/top"
... />
</LinearLayout>
Typically, in this vertical layout, starting the manipulation from the first button (focus) will not run anywhere, and if you start from the second button, the (focus) will not go under it. Now the top button defines a (button) at the bottom as well as the Nextfocusup setting (and vice versa), and the focus focal point will move up and down-on the loop. If you want to have a view in your UI that is focusable (when it is not generally the case), add
android:focusable
XML attributes, declared in your layout file. Set this value to true. You can also declare the focus of the view, when in touch mode
android:focusableInTouchMode
To get the focus for a particular view, call Requestfocus () to listen for the Focus event (notify when a view Gets or loses focus), and use Onfocuschange (), which is discussed in the event listener in the above # events Listeners.
Android Input events (input event)