Android soft keyboard summary

Android soft keyboard properties

Soft keyboard property usually defined in AndroidManifest.xml Activity at node attribute called android: windowSoftInputMode  This property has two optional components, is at the beginning of a "state" to display the hidden software specified disk, is a " adjust "the beginning of the Activity window used to specify the adjustment mode, convenient display the soft keyboard. You can specify one, two or specify the value of using |  to separate.

windowSoftInputMode values, as follows:

state at the beginning:

  • stateVisible: indicates that when the state is always soft keyboard to enter the Activity pop-up display.
  • stateAlwaysVisible: indicates that when the state is always soft keyboard to enter the Activity pop-up display, even jump to another screen, the soft keyboard interface is hidden state, then jumps back to the soft keyboard will still appear in the pop-up.
  • stateHidden: it means that when entering the Activity soft keyboard is always hidden state.
  • stateAlways Hidden : means that when entering the Activity soft keyboard is always hidden state, even jump to another screen, the soft keyboard interface status is displayed, and then jump back in the soft keyboard remains hidden.
  • stateUnchanged: Indicates the current state of the same soft keyboard, soft keyboard if the status of the other interface is displayed when jumping the soft keyboard interface status is displayed, if it is hidden it is hidden.
  • stateUnspecified: indicates an unspecified state, the system state will be defined according to the appropriate interface.

adjust the beginning:

  • adjustPan: This attribute will interface layout shifted upward, so that the current is not blocked soft keyboard focus, but if the following there will be blocked by the input box, the user must put away below and click the soft keyboard input box again.
  • adjustResize: This property is always Activity resizing the main window to make room for the soft keyboard, which can display the content itself needs Activity can be adjusted, it would not work.

Soft keyboard occlusion

  • Method a: windowSoftInputMode Configuration
    1. Configuration adjustPan: layout will be go on top, if there is below the input box will still be blocked, the user experience is not good.
    2. + AdjustResize ScrollView : adjustResize required layout height may vary, so ScrollView added in the layout.

              But when the set full-screen or immerse the status bar will fail.

              Setting Full Screen

              When set  getWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN) , the screen when more than one input box, the current frame input following input box and can not slide down input, similar to the case adjustPan, only You can slide portion, and does not adjust the height of the interface.

               Set immersive status bar

               Was added in the following style  : <item name = "android windowTranslucentStatus "> true </ item> after clicking the input box when the shutter input box pop-up software, by setting the root layout android: fitsSystemWindows = "true"  to resolve failures.

  • Method two: by listening to the top of the Activity View to determine whether the soft keyboard pops up, and then re-drawn interface

    public class SoftHideKeyBoardUtil {
        public static void assistActivity(Activity activity) {
            new SoftHideKeyBoardUtil(activity);
        }
    
        private View mChildOfContent;
        private int usableHeightPrevious;
        private FrameLayout.LayoutParams frameLayoutParams;
    
        private SoftHideKeyBoardUtil(Activity activity) {
            //1、找到Activity的最外层布局控件,它其实是一个DecorView,它所用的控件就是FrameLayout
            FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
            //2、获取到setContentView放进去的View
            mChildOfContent = content.getChildAt(0);
            //3、给Activity的xml布局设置View树监听,当布局有变化,如键盘弹出或收起时,都会回调此监听
            mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
                //4、当前布局发生变化时,对Activity的xml布局进行重绘
                possiblyResizeChildOfContent();
            });
            //5、获取到Activity的xml布局的放置参数
            frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
        }
    
        // 获取界面可用高度,如果软键盘弹起后,Activity的xml布局可用高度需要减去键盘高度
        private void possiblyResizeChildOfContent() {
            //1、获取当前界面可用高度,键盘弹起后,当前界面可用布局会减少键盘的高度
            int usableHeightNow = computeUsableHeight();
            //2、如果当前可用高度和原始值不一样
            if (usableHeightNow != usableHeightPrevious) {
                //3、获取Activity中xml中布局在当前界面显示的高度
                int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
                //4、Activity中xml布局的高度-当前可用高度
                int heightDifference = usableHeightSansKeyboard - usableHeightNow;
                //5、高度差大于屏幕1/4时,说明键盘弹出
                if (heightDifference > (usableHeightSansKeyboard / 4)) {
                    // 6、键盘弹出了,Activity的xml布局高度应当减去键盘高度
                    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                } else {
                    frameLayoutParams.height = usableHeightSansKeyboard;
                }
                //7、 重绘Activity的xml布局
                mChildOfContent.requestLayout();
                usableHeightPrevious = usableHeightNow;
            }
        }
    
        private int computeUsableHeight() {
            Rect r = new Rect();
            mChildOfContent.getWindowVisibleDisplayFrame(r);
            // 全屏模式下:直接返回r.bottom,r.top其实是状态栏的高度
            return r.bottom;
        }
    }复制代码


Reproduced in: https: //juejin.im/post/5cf4c55b6fb9a07f0a2dca68

Guess you like

Origin blog.csdn.net/weixin_34261739/article/details/91430542