Solution to interface jitter when switching between Android input method and expression panel

Yesterday, I thought about the input method pop-up mode of Android, and suddenly found that using the pop-up mode of dynamic switching input method can solve the problem of input method jitter. What is the specific jitter? Let's first look at the negative textbook of microblog.

[specifically, the height of the expression panel is inconsistent with that of the input method panel, resulting in that when the input method pops up (the layout is squeezed), the expression panel needs to be hidden (the layout is pulled up), resulting in height difference jitter on the interface, so there will be a bad jitter experience during switching)]

After using the solution to solve jitter, the effect is as follows:

[this scheme is obviously smoother than microblog switching]

As usual, let's talk about ideas first. We mainly use two input method pop-up modes: adjustresize (adjustment mode) and adjustnothing (no adjustment). (for more information, please refer to my previous article: input method pop-up parameter analysis)

1. In the initial situation (the keyboard and expression panel are not expanded): we set a default height for the expression panel (because we don't know how high the keyboard is) and set the input pop-up mode to adjustresize mode. 2. When we click EditText, the system will pop up the input method. Since the mode we set before is adjustresize, the input method will squeeze layout, and the extrusion height will eventually be fixed to a value (keyboard height). When we detect the extrusion, record the extrusion difference (i.e. keyboard height) as the new height value of the expression panel. At the same time, we hide the expression panel. 3. When we click the expression button, we need to judge whether the input method has been expanded. 1) If it has been expanded, our task is to hide the keyboard smoothly and display the expression panel. The specific method is as follows: first set the input method pop-up mode of activity to adjustnothing, then take the keyboard height recorded in the previous step as the height of the expression panel, and then display the expression panel. At this time, because the keyboard pop-up mode is adjustnothing, the keyboard will not shake, and because the expression panel is as high as the keyboard, EditText will not move down, Finally, hide the input method. 2) If the input method is not expanded, we will judge whether the expression panel is expanded. If it is expanded, it will be hidden and the input method pop-up mode will be reset to adjustresize. If it is not expanded, it will be directly displayed and the input method pop-up mode will be set to adjustnothing. The general implementation idea is mentioned above. However, since we are ready to do the help class, we will click the blank space to fold the keyboard and the expression panel together. The specific implementation idea is as follows: mask a layer of FrameLayout on the decorview of activity to monitor the Acton of touch_ Down event. If it is outside the input range, the expression panel and keyboard are folded. The schematic diagram is as follows:

When you're done, let's go.

1. Create the inputmethodutils class. The construction method needs to pass the activity parameter, declare the required member variables, and implement the view.onclicklistener interface (because we want to listen to the click event of the expression button). The code is as follows:

[moved the bricks and will continue to write later...] well, continue to write

2. Before moving on, we have to consider how to design expression buttons, expression button click events, and expression panels. My approach is to create an internal viewbinder class. (because logically, the three are integrated) the implementation code of viewbinder is as follows:

Ontriggerclicklistener is used to solve the problem that the trigger occupies the listener (our internal logic needs to occupy the listener. If the external wants to implement additional click logic, we can't add a listener to the trigger, so ontriggerclicklistener is used to replace the onclicklistener of the original sound). Ontriggerclicklistener is an interface, and the implementation code is as follows:

3. After implementing viewbinder, we also need to implement a mask view to listen to action_ Down event. The code is as follows:

4. After the preparation, we can continue to improve the inputmethodutils class. Since we need to store the viewbinder object (mainly used for the association between control buttons and panels), we also need to declare a collection in inputmethodutils. The code is as follows:

5. It is necessary to write some common methods followed by viewbinders (such as folding all expression panels, obtaining which expression panel is currently expanded, etc.), and the code is as follows:

6. Judge whether the input method has been expanded by listening to the change of layout. The code is as follows:

7. Common methods to display / hide the keyboard and dynamically control the pop-up mode of input method. The code is as follows:

8. Initialize these components in the construction method and make relevant settings. The code is as follows:

[suddenly something happens, write here first and improve later...] come back and write again. The above code basically meets the requirements. What we need to focus on is how to detect the pop-up / hidden state of the keyboard (some people may say to use inputmethodmanager. Isactive(), ah, en... Anyway, if I have this method problem, he will always return true to me). Here is a brief introduction to how to detect the pop-up and hidden state of the keyboard.

1. If the current input method is in the adjustresize mode, we can directly use the deformation monitoring of layout, that is, the code previously implemented by detectkeyboard().

2. If the current input method is in adjust note mode, this is a little difficult to handle, because there is no deformation to monitor. My implementation method is to judge the action through the mask view_ The coordinate of down. If the coordinate falls in the input box (that is, when the user clicks the input box, the system will pop up the input box), then we can consider the keyboard as the pop-up mode. The code is embodied in the dispatchtouchevent () method of closekeyboardonoutsidecontainer.

At this point, the development is over. By convention, the complete code is as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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