The Android app has added a transparent bar effect in the past two days. It turns out that the keyboard pops up and will cover the EditText at the bottom of the screen. It does not resize the window like you imagined, and scrolls the ScrollView to display the EditText above the keyboard. I have encountered similar problems before, so after solving them, I simply wrote down all the problems and solutions between EditText and the keyboard to check them later.
1. Before 5.0, if EditText sets gravity="center|right" and singleLine="true" is set at the same time, it will cause EditText to continuously click on the keyboard to pop up, and the EditText will be blocked from the second meeting.
5.0+ will not have this problem, and the solution is simple. Just wrap the ScrollView on the outer layer of EditText and set the keyboard mode to adjustResize mode (both are indispensable).
2. According to the solution described above, there was no problem. Until recently, the transparent bar effect was added, and the keyboard mode seemed to be invalid directly. Every time you click the bottom EditText, the keyboard pops up from the bottom, the window size will not be adjusted, and the bottom EditText will also be covered. Finally, it was found that when the getWindow().getDecorView().setSystemUiVisibility method is set with SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION, or (.FLAG_TRANSLUCENT_STATUS), it will cause the EditText to be covered by the keyboard.
Because the transparent bar effect is supported from 4.4, this bug has occurred in the project since 4.4. Of course, only the transparent bar effect of 5.0+ can be considered. You can directly set the status bar color to achieve the transparent bar effect. Since the project needs to be supported from 4.4, this problem must be solved.
Under normal circumstances, the system UI will occupy some space for the app, such as the status bar, keyboard, navigation bar, etc., which means that our app UI will not appear under the system UI. However, according to the test results, in order to occupy the status bar space or full screen, some of the above attributes will be overwritten by the system UI.
Solution:
Listen to the layout changes of the interface container. When it changes, check the height of the visible area in the window to determine whether the keyboard is popping up. If it pops up, modify the container bottom padding, that is, manually implement the adjustResize effect to leave space for the keyboard to display space. In this way, the ScrollView will automatically resize and display EditText on the top of the keyboard.
public class KeyboardPatch { private Activity activity; private View decorView; private View contentView; /** * Constructor * @param act The activity that needs to be resolved * @param contentView interface container, usually in activity, or may be a Fragment container, passed according to personal needs * */ public KeyboardPatch(Activity act, View contentView) { = act; = ().getDecorView(); = contentView; } /** * Listen to layout changes * */ public void enable() { ().setSoftInputMode(.SOFT_INPUT_STATE_HIDDEN | .SOFT_INPUT_ADJUST_RESIZE); if (.SDK_INT >= 19) { ().addOnGlobalLayoutListener(onGlobalLayoutListener); } } /** * Cancel monitoring * */ public void disable() { ().setSoftInputMode(.SOFT_INPUT_STATE_HIDDEN | .SOFT_INPUT_ADJUST_PAN); if (.SDK_INT >= 19) { ().removeOnGlobalLayoutListener(onGlobalLayoutListener); } } private onGlobalLayoutListener = new () { @Override public void onGlobalLayout() { Rect r = new Rect(); (r); int height = ().getResources().getDisplayMetrics().heightPixels; int diff = height - ; if (diff != 0) { if (() != diff) { (0, 0, 0, diff); } } else { if (() != 0) { (0, 0, 0, 0); } } } }; }
The above is the method of handling Android EditText being covered by soft keyboards introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!