Get the status and height of soft keyboard in Android

Get the status and height of soft keyboard in Android

Application scenario

In Android applications, it is sometimes necessary to obtain the status of the soft keyboard (that is, whether the soft keyboard is displayed or hidden) and the height of the soft keyboard. Here are some possible application scenarios.

Scene 1

When the soft keyboard is displayed, pressing the return key should retract the soft keyboard rather than go back to the previous interface, but some models have bugs in the processing of the return key. After pressing the return key, although the soft keyboard will automatically retract, it will not consume the return event, resulting in the activity receiving the return event and performing the return operation. At this time, it needs to be judged, If the soft keyboard has just changed from displayed to hidden, the fallback operation is not performed.

Scene 2

When the soft keyboard pops up, it will block most of the bottom to the middle of the interface. If users want to view and operate the covered area, they must first put away the soft keyboard, which will affect user interaction. Therefore, it is usually necessary to move some views at the bottom, such as buttons, to the top of the soft keyboard after the soft keyboard pops up to facilitate user operation.

API dilemma

The Android SDK does not provide any API to directly obtain the status and height of the soft keyboard. Many online materials say that the isactive() method of inputmethodmanager can obtain the status of the soft keyboard. However, the actual test shows that this method is useless. If it returns false, it can be judged that the soft keyboard must be hidden, but if it returns true, The soft keyboard may be either displayed or hidden. Therefore, the isactive () method cannot be used to determine whether the soft keyboard is displayed or hidden. In order to obtain the status and height of the soft keyboard, it can only be achieved through indirect methods.

Register layout change monitoring

In Android, when the soft keyboard changes from hidden to displayed, or from displayed to hidden, it will trigger the global layout change of view in the current layout. The status of the soft keyboard can be known by monitoring the changes of the global layout.

The Android framework provides a viewtreeobserver class, which is an observer class of the view tree. A series of public interfaces are defined in the viewtreeobserver class. When a view attaches to a window, a viewtreeobserver object will be created. In this way, when the view tree of a view changes, a method of the object will be called to notify each registered listener of the event.

Ongloballayoutlistener is one of many interfaces defined in viewtreeobserver. It is used to listen for changes in the global layout of a view tree or changes in the visual state of a view in the view tree. When the soft keyboard changes from hidden to displayed or from displayed to hidden, the dispatchongloballayout() method of the viewtreeobserver object in all existing views in the current layout will be called. In this method, all registered ongloballayoutlisteners will be traversed, the corresponding callback method will be executed, and the message of global layout change will be notified to each registered listener.

The method of registering ongloballayoutlistener with viewtreeobserver in a view is as follows.

There are some things to pay attention to when registering ongloballayoutlistener.

In order to accurately judge whether it is caused by the change of soft keyboard state in the callback of ongloballayoutlistener and obtain the height of soft keyboard, another interface is required.

Gets the size of the display area visible to the current window

A method getwindowvisibledisplayframe() is provided in the view. This method will return the visible area size of the window to which the view is attached. When the soft keyboard is displayed, the visible area size of the window will be compressed. When the soft keyboard is hidden, the visible area size of the window will be restored. However, not only the display and hiding of the soft keyboard will affect the size of the visible area of the window. For example, most tablets and some mobile phones have a row of virtual keys (virtual return key, home key, etc.), and the display and hiding of virtual keys will also change the visible area of the window. However, fortunately, in addition to the soft keyboard, the impact of other operations on the visible area of the window does not account for a large proportion of the whole screen size. By setting a reasonable threshold, we can accurately judge whether the layout change is caused by the display and hiding of the soft keyboard. In addition, getwindowvisibledisplayframe () will return the height of the visible area of the window. By subtracting it from the screen height, you can get the height of the soft keyboard.

Monitor the status change of the soft keyboard

After obtaining the status and height of the soft keyboard, you can perform the required operations. For example, rearrange the button position, set variables, record the current soft keyboard state and the last soft keyboard hiding time, etc. However, if multiple classes need to perform some operations according to the soft keyboard state, it is troublesome and unnecessary to do so in each class. At this time, you can define an interface to monitor the soft keyboard state change in the main activity. Other classes interested in the soft keyboard state can register the soft keyboard state change monitoring in the main activity. In the main activity, the listener is notified when the soft keyboard state changes.

Complete sample code

The complete example code is as follows.

Screenheight is the screen height. There are many methods to obtain the screen height on the Internet, which will not be introduced here.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to 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
分享
二维码
< <上一篇
下一篇>>