软键盘挡住输入框问题的精确解决方案

最近给公司开发App,体验的人员觉得登陆界面总是被软键盘挡住了不爽,自行度娘了一大堆发现都是基于Scrollview的或者是把界面整体拔高。但是我下面有注册按钮我拔高了又太丑了。
所以感觉不爽,在看了郭霖大神发的推送后根据GoogleBugs里解决方案改写了里面的解决方案,改为了一套能够移动部分控件的工具,并且提高了精度。

附上链接:SoftKeyBoardAdapter

package com.example.hele.softkeyboardadapter;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.FrameLayout;

/**
 * Created by Hele on 2017/5/8.
 * 修改自http://mp.weixin.qq.com/s/sWnxIxzNkuTkUaVaPhtqLA
 */
public class SoftHideKeyBoardUtil {

    private static boolean sTranslucentStatus = false;
    private Activity mContext;
    //Root Content View
    private View mChildOfContent;
    private View mViewContainer;
    private int mUsableHeightPrevious;
    private int mLastLocation = -1;
    private int mFrameSize = -1;

    private SoftHideKeyBoardUtil(Activity activity, View container) {
        mContext = activity;
        mViewContainer = container;
        sTranslucentStatus = judgeTranslucentStatus(activity);
        //获取根框
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        //获取ContentView
        mChildOfContent = content.getChildAt(0);
        //ViewTreeObserver:监听界面绘制
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
    }

    /**
     * Call this method to prevent your view such as EditText, LoginButton or other
     * from being blocked by a soft keyboard
     *
     * @param activity               : current activity
     * @param view_container_to_move : your view group
     */
    public static void assistActivity(Activity activity, View view_container_to_move) {
        new SoftHideKeyBoardUtil(activity, view_container_to_move);
    }

    private static boolean judgeTranslucentStatus(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if ((WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS & activity.getWindow().getAttributes().flags)
                    == WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) {
                return true;
            }
        }
        return false;
    }

    /**
     * Adjust the location of your container
     */
    private void possiblyResizeChildOfContent() {
        if (mLastLocation < 0) {
            mLastLocation = (int) mViewContainer.getY();
            //Use the beginning as default
            mUsableHeightPrevious = computeUsableHeight();
            mFrameSize = mUsableHeightPrevious;
        }
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != mUsableHeightPrevious) {
            int heightKeyboard = mFrameSize - usableHeightNow;
            int heightDifference = mUsableHeightPrevious - usableHeightNow;

            float adjustY = mLastLocation;

            //监听键盘变化
            if (heightKeyboard > (mFrameSize / 4) && heightDifference > (mFrameSize / 4)) { 
                //第二个条件是必须的,判断键盘弹起
                //When full screen or translucentStatus is true
                int statusBar = sTranslucentStatus ? DisplayUtil.getStatusBarHeight(mContext) : 0;
                adjustY = mViewContainer.getY() - mViewContainer.getBottom() + usableHeightNow + statusBar;
            } else if (heightKeyboard == 0) {
                //收起键盘
            } else {
                //中英文切换
                //中文切英文 : dif < 0 . 反之, dif > 0
                adjustY = mViewContainer.getY() - heightDifference;
            }
            mViewContainer.setY(adjustY);
            mChildOfContent.requestLayout();
            mUsableHeightPrevious = usableHeightNow;
        }
    }

    /**
     * Compute Visible Height
     * @return
     */
    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }

}

后来发现有人解释过了所以我就不重新解释一遍了。
附上链接: 解释链接

除了possiblyResizeChildOfContent方法,其他次要内容和Google上面的差不多。主要是添加了渲染模式的处理、中英文的切换。如果采用原来的方式用在内部控件是无法处理中英文切换的,有兴趣的话可以尝试一下。

你可能感兴趣的:(软键盘挡住输入框问题的精确解决方案)