Keyboard input box shield bug

Today resolved a bug, virtual keyboard blocked entry box.
 

There are several approaches to solve such problems, but also different situations for different projects practice

Problems like the input box at the bottom of the keyboard pops up when the input box is blocked

There are several ideas to solve , there are several points need to be noted, do it later explain, but also a lot of reference information network again express my gratitude

A simple set of common when windowSoftInputModel = directly in the manifest "adjustPan | stateHidden"

This trap can be used in the layout Scorllview. Of course, nowhere is set according to a variety of different topics and pay attention, said online ScrollView in fillViewPort = true, it is said fitSystemWindow = true situation. Here is my situation use. Comprehensive give you some advice.

 

A, the entire activity from the top, the purpose is not shielded by the

public class AndroidBug5497Workaround {

    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    public static void assistActivity (Activity activity) {
        new AndroidBug5497Workaround(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497Workaround(Activity activity) {
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard/4)) {
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);// 全屏模式下: return r.bottom
    }

}

To achieve this is to move the entire activity is not blocked, is to look at the code known to do the processing activity of setContentView

This situation is very convenient, but I changed someone using an invalid bug, is that the project theme set the background transparent, there is a bug in this way, when the virtual keyboard pops up, turn off the keyboard then there will be a transparent background. If no such themes transparent case there is no problem it is also very convenient.

3. The second to set the layout of the padding value the following code, when talking to elevate the bottom of the keyboard, I can be considered for this case, but if they set up full-screen mode in activity in all cases I have mentioned are invalid .
 

public class KeyboardPatch {
    private Activity activity;
    private View decorView;
    private View contentView;

    /**
     * 构造函数
     * @param act 需要解决bug的activity
     * @param contentView 界面容器,activity中一般是R.id.content,也可能是Fragment的容器,根据个人需要传递
     * */
    public KeyboardPatch(Activity act, View contentView)
    {
        this.activity = act;
        this.decorView = act.getWindow().getDecorView();
        this.contentView = contentView;
    }

    /**
     * 监听layout变化
     * */
    public void enable()
    {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        if (Build.VERSION.SDK_INT >= 19)
        {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    /**
     * 取消监听
     * */
    public void disable()
    {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        if (Build.VERSION.SDK_INT >= 19)
        {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    private ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            Rect r = new Rect();

            decorView.getWindowVisibleDisplayFrame(r);
            int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
            int diff = height - r.bottom;

            if (diff != 0)
            {
                if (contentView.getPaddingBottom() != diff)
                {
                    contentView.setPadding(0, 0, 0, diff);
                }
            }
            else
            {
                if (contentView.getPaddingBottom() != 0)
                {
                    contentView.setPadding(0, 0, 0, 0);
                }
            }
        }
    };
}

 This can be set contentview Depending on your situation, look at the code is set padding elevate the value of the virtual keyboard pops up, closed padding value is zero.

 

If we have welcomed the idea to the point

Guess you like

Origin blog.csdn.net/chengzuidongfeng/article/details/85342733