KeyEvent class

The Android.view.keyevent class defines a set of constants and methods that describe keystroke events in Android. Constants and methods associated with the return key are:

  • Keyevent. KEYCODE_BACK: indicates that the key type is a return key
  • Keyevent. ACTION_DOWN: indicates that the event is when the key is pressed. If you hold down the key for a long time, the event will continue to be generated.
  • Keyevent. ACTION_UP: indicates that the event is to release the key. Once you click the key, the procedure will be called only once.
  • Public Final int getKeyCode() : Gets the key type corresponding to this event.
  • Public Final int getAction() : Gets the event type of this event

Intercepts the return key in the Activity

The return key can be intercepted by overriding the onKeyDown and onKeyUp methods in the Activity’s derived classes.

public boolean onKeyDown(int keyCode, KeyEvent event);
public boolean onKeyUp(int keyCode, KeyEvent event);
Copy the code

Both methods take two arguments, the first of which is keyCode, the key type corresponding to this event. The second parameter is the event object, and the event is used to get the details of the event. Event.getaction () in onKeyDown() always returns keyevent.action_down, and event.getAction() in onKeyUp() always returns keyevent.action_up. If you want to intercept the return key, add the following code to both methods.

if (keyCode == KeyEvent.KEYCODE_BACK) {
    ...
}
Copy the code

Dialog intercepts the return key

A Dialog can be added to listen for key events by calling the setOnKeyListener() method. The prototype setOnKeyListener() method is:

public void setOnKeyListener(final OnKeyListener onKeyListener);

This method takes one parameter that implements the OnKeyListener interface. The OnKeyListener interface is defined as follows.

interface OnKeyListener {
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event);
}
Copy the code

The onKey() method takes three arguments, the first of which is a reference to the dialog object that intercepted the event. The second argument is the keyCode corresponding to the event, and the third argument is the event object itself. If you want to intercept the return key, add the following code to the Dialog.

setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK 
            && event.getAction() == KeyEvent.ACTION_UP) {
            ...
        }
        return false; }});Copy the code

Intercept return key events in EditText

In EditText, you can also add a listener for key events by calling the setOnKeyListener() method. The setOnKeyListener() method is used exactly as in Dialog.

View intercepts return key events

The setOnKeyListener() method can be called on all View derived objects to increase listening for key events, but other views, except EditText, do not set listening. Keystroke events are not distributed to the View when generated.

Conflict and selection of multiple interception events

Intercepting events can now be successfully set up in the Activity, Dialog, and EditText. If multiple objects have interception events set. Events are distributed to only one object.

The results are as follows:

Dialog has the highest priority. If there is an Activity, the Activity pops up a Dialog, and the Dialog has a EditText, set the listener in the Activity, Dialog, and EditText. Only the listening procedure set up in the Dialog executes correctly. The listening procedure in the Activity and EditText cannot be executed.

2. If you have an Activity and an EditText in the Activity, set the listener in both the Activity and EditText, and only the listener set in the Activity will execute correctly. The listening procedure in the EditText cannot be executed.

3. If PopupWindow is present in the interface, press the back key and PopupWindow will receive an event notification and consume (dismiss();). . Other listening objects cannot get event notifications. (PopupWindow internal layout class PopupViewContainer overwrites the dispatchKeyEvent() method.)

Returns the key response speed limit

After the user presses the back key, if the interface is stuck and the interface does not complete the return action immediately, the user may think that the operation failed and press the back key again. This causes the return event to be called again. When the delay is over, the phenomenon of multiple returns occurs. To avoid this, add a time limit to the function that intercepts the return key. That is, if the time between the return event and the last processing time is too long, the event is not processed. Direct return true; Consume this event. Using the example of intercepting the return key in a dialog box, the code to increase the response speed limit of the return key is as follows.

setOnKeyListener(new OnKeyListener() {
    private static final int INTERVAL = 500;   // Response interval time
    private long lastReturnTime;               // The last response returns the event time
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK 
            && event.getAction() == KeyEvent.ACTION_UP) {
            long curTime = System.currentTimeMillis();
            if(curTime - lastReturnTime > INTERVAL) { lastReturnTime = curTime; . }else {
                return true; }}return false; }});Copy the code