Detailed examples of Android intercepting return key events

Detailed examples of Android intercepting return key events

Keyevent class

A series of constants and methods are defined in the android.view.keyevent class to describe the events in Android

There are constants and methods related to key events and return keys.

Intercept return key in activity

In the derived class of activity, the return key can be intercepted by overriding onkeydown and onkeyup. The prototypes of these two methods are.

Both methods have two parameters. The first parameter is keycode, that is, the key type corresponding to this event. The second parameter is the event object. You can get the details of the event through event. In onkeydown() method, event. Getaction() always returns keyevent. Action_ The event. Getaction () in the down, onkeyup () method always returns keyevent. Action_ UP。

If you want to intercept the return key, add the following code to the two methods.

Intercept return key in dialog

In dialog, you can call setonkeylistener() method to add key event listening for dialog.

The prototype of setonkeylistener() method is:

This method has a parameter that needs to implement the onkeylistener interface. Onkeylistener interface is defined as follows.

The onkey () method contains three parameters. The first parameter is the reference of the dialog object that intercepts this event. The second parameter is the keycode corresponding to this event, and the third parameter is the event object itself.

If you want to intercept the return key, add the following code to the dialog.

Intercept return key events in EditText

In EditText, you can also call the setonkeylistener () method to add key event listening for EditText. The setonkeylistener () method is used exactly as in dialog.

Intercept return key events in view

Setonkeylistener() method can be called on all view derived class objects to increase the listening of key events. However, except EditText, the listening set by other views will not work. When the key event is generated, it will not be distributed to the view.

Conflict and selection of multiple interception events

At present, intercepting events can be successfully set in activity, dialog and EditText. If multiple objects are set to intercept events. Events are distributed to only one object. The following conclusions are obtained through experiments:

1. Dialog has the highest priority. If there is an activity, a dialog pops up in the activity, and there is an EditText in the dialog. Listening is set in activity, dialog and EditText. Only the listening process set in dialog can be executed correctly. The listening process in activity and EditText cannot be executed.

2. The priority of activity is lower than dialog, but higher than EditText. If there is an activity, there is an EditText in the activity. Listening is set in both activity and EditText. Only the listening process set in activity can be executed correctly. The listening process in EditText cannot be executed.

3. If there is a popupwindow in the current interface, press the return key and popupwindow will receive the event notification and consume (execute dismiss();). Other objects with listening settings cannot get event notifications. (the reason is that the popupwindow internal layout class popupviewcontainer overrides the dispatchkeyevent() method)

Return key response speed limit

When the user presses the return key, if the interface is stuck, resulting in the interface not completing the return action immediately, the user may feel that the pressing operation is unsuccessful and press the return key again. This causes the return event to be called again. When the Caton ended, there was a phenomenon of multiple returns. To avoid this, you can add a time limit to the function that intercepts the return key. That is, if the time between the current return event and the last processing is too long, the current event will not be processed. Directly return true; Consume this event.

Take intercepting the return key in the dialog box as an example. The code for increasing the response speed limit of the return key is as follows.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. I hope this article can help you. Thank you for reading and your support for this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>