Implementation process of display and hiding function of Android soft keyboard

1、 Principle of soft keyboard display

What is the essence of software disk? The soft keyboard is actually a dialog!

Inputmethodservice creates a dialog for our input method, and sets some parameters (such as gravity) of the window of the dialog so that it can be displayed at the bottom or full screen. When we click the input box, the system adjusts the active main window to make room for the input method, and then displays the dialog at the bottom or full screen.

2、 Active main window adjustment

Android defines an attribute named windowsoftinputmode, which allows the program to control the adjustment of the active main window. We can set the activity in androidmanifest.xml. For example: Android: windowsoftinputmode = "stateunchanged|adjustpan" the optional value of this attribute has two parts: one is the state control of the soft keyboard, and the other is the adjustment of the active main window. The previous part of this article will not be discussed. Please refer to the Android documentation yourself.

Mode 1, compression mode

If the value of windowsoftinputmode is set to adjustresize, the main window of the activity is always resized to make room for the soft keyboard.

We use a piece of code to test what the system does when the input method pops up after we set this attribute. Override layout:

xml:

Activity setting property of androidmanifest.xml: Android: windowsoftinputmode = "adjustresize"

Run the program, click the text box to view the debugging information:

From the debugging results, we can see that when we click the text box, the root layout calls onmeasure, onsizechanged and onlayout.

In fact, when it is set to adjustresize, when the soft keyboard pops up, measure and layout the main window layout again. During layout, the size of the window changes, so onsizechanged is called.

From the running results in the figure below, we can also see that the textview originally at the bottom is pushed to the top of the input method.   

Mode 2, translation mode

If the value of windowsoftinputmode is set to adjustpan, the main window of the activity does not resize the screen to make room for the soft keyboard. On the contrary, the content of the current window will automatically move so that the current focus is never covered by the keyboard and the user can always see the part of the input content. This is usually not expected than resizing, because the user may turn off the soft keyboard to obtain interaction with the overwritten content. In the above example, we change the property of androidmanifest.xml: Android: windowsoftinputmode = "adjustpan"

Rerun and click the text box to view the debugging information:

We can see that the system also performs the measurement and layout again, but we find that onsizechanged is not called during the layout process, which indicates that the size of the original layout is not changed before and after the input method pops up.

From the running results in the figure below, we can see that the textview below is not pushed above the input method.

In fact, this mode does not adjust the layout when the input box is not occluded, but the window will translate when the input box is about to be occluded. That is, the mode always keeps the input box visible. As shown below, the whole window, including the title bar, is moved up to ensure that the text box is visible

Mode 3 automatic mode

When the property windowsoftinputmode is set to adjustuspecified, it is not specified whether the main window of the activity is resized to make room for the soft keyboard, or whether the content on the window is visible with the current focus on the screen. The system will automatically select one of these modes, which mainly depends on whether any layout view of the contents of the window can scroll their contents. If there is such a view, the window will be resized. This assumption can make the contents of the scrolling window visible in a smaller area. This is the default behavior setting of the main window.

In other words, the system automatically determines whether to use translation mode or compression mode. The decisive factor is whether the content can be scrolled.

The above is the implementation process of the display and hiding function of Android soft keyboard introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>