Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android虚拟键盘适配 #22

Open
lyxxman opened this issue Nov 13, 2018 · 0 comments
Open

android虚拟键盘适配 #22

lyxxman opened this issue Nov 13, 2018 · 0 comments

Comments

@lyxxman
Copy link
Owner

lyxxman commented Nov 13, 2018

方案一:
针对一般界面,使用主题设置,就行了。
方案二:
全屏页面或者沉侵式状态栏页面,可以使用下面的方案:
public class NavigationBarUtil {
private static NavigationBarUtil instance;

public static void initActivity(View content, boolean isChengQingShi) {
    instance = new NavigationBarUtil(content, isChengQingShi);
}

public static void destroy() {
    if (instance != null) {
        instance.destoryed();
        instance = null;
    }
}

public static NavigationBarUtil getInstance(View content, boolean isChengQingShi) {
    if (instance == null) {
        instance = new NavigationBarUtil(content, isChengQingShi);
    }
    return instance;
}

private View mObserved;//被监听的视图
private int usableHeightView;//视图变化前的可用高度
private ViewGroup.LayoutParams layoutParams;
private boolean isChenQingShi = false;

private NavigationBarUtil(View content, final boolean isChenQingShi) {
    mObserved = content;
    this.isChenQingShi = isChenQingShi;
}

public void register() {
    //给View添加全局的布局监听器监听视图的变化
    mObserved.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            resetViewHeight(isChenQingShi);
        }
    });
    layoutParams = mObserved.getLayoutParams();
}

public void unRegister() {
    //给View添加全局的布局监听器监听视图的变化
    mObserved.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
        }
    });
    layoutParams = mObserved.getLayoutParams();
}

private void destoryed() {
    mObserved = null;

}

/**
 * 重置视图的高度,使不被底部虚拟键遮挡
 */
private void resetViewHeight(boolean isChenQinShi) {
    int usableHeightViewNow = CalculateAvailableHeight(isChenQinShi);
    //比较布局变化前后的View的可用高度
    if (usableHeightViewNow != usableHeightView) {
        //如果两次高度不一致
        //将当前的View的可用高度设置成View的实际高度
        layoutParams.height = usableHeightViewNow;
        mObserved.requestLayout();//请求重新布局
        usableHeightView = usableHeightViewNow;
    }
}

/**
 * 计算试图高度
 *
 * @return
 */
private int CalculateAvailableHeight(boolean isChegnQingShi) {
    Rect r = new Rect();
    mObserved.getWindowVisibleDisplayFrame(r);

// return (r.bottom - r.top);//如果不是沉浸状态栏,需要减去顶部高度
if (isChegnQingShi) {
return (r.bottom);//如果是沉浸状态栏
} else {
return (r.bottom - r.top);
}

}

/**
 * 判断底部是否有虚拟键
 *
 * @param context
 * @return
 */
public static boolean hasNavigationBar(Context context) {
    boolean hasNavigationBar = false;
    Resources rs = context.getResources();
    int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) {
        hasNavigationBar = rs.getBoolean(id);
    }
    try {
        Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
        Method m = systemPropertiesClass.getMethod("get", String.class);
        String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
        if ("1".equals(navBarOverride)) {
            hasNavigationBar = false;
        } else if ("0".equals(navBarOverride)) {
            hasNavigationBar = true;
        }
    } catch (Exception e) {

    }
    return hasNavigationBar;

}

}
使用方法:
在oncreateView里面判断是否有虚拟键盘就行。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant