Android软键盘遮住输入框的解决方法终极适配

新的一周,新的开始迎接新的挑战。今天简单介绍一下Android输入框被软键盘遮住的两种常用方法。

法一:清单文件对要进行设置的Activity属性配置android:windowSoftInputMode="adjustPan|adjustResize"

但经过测试会发现有时候标题栏也会被挤上去,怎么解决这样的问题呢 ?如果用法一的话这样的问题貌似解决不了

法二:

还有一种方法可以解决这样的问题 这种也是我在网上找的做下记录下次再用的话也不用去网上找了 ,首先把标题栏抽取到基类里面进行初始化等一些列的操作 ,然后对当前的Activity在OnCreate方法setContentView下配置AndroidBug5497Workaround.assistActivity(this);即可

AndroidBug5497Workaround可以放在工具类中,它的代码如下

public class AndroidBug5497Workaround {

public static void assistActivity (Activity activity) {

new AndroidBug5497Workaround(activity);

    }

private ViewmChildOfContent;

    private int usableHeightPrevious;

    private FrameLayout.LayoutParamsframeLayoutParams;

    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

    }

}

这种方法就可以解决标题栏不会被挤上去的情况了

你可能感兴趣的:(Android软键盘遮住输入框的解决方法终极适配)