美文网首页
解决当activity全屏时 使用adjustResize失

解决当activity全屏时 使用adjustResize失

作者: 给你一刀 | 来源:发表于2017-10-23 23:20 被阅读0次
/*
 * File Name: KeyboardUtil.java 
 * History:
 * Created by mulinrui on 2015年8月26日
 */
package com.zhiyoo.util;

import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;

import com.anzhi.common.util.VersionUtils;
import com.zhiyoo.ui.MarketBaseActivity;

/**
 * 解决当activity全屏时  使用adjustResize失效的问题
 * 造成弹出的输入法遮挡底部输入框,或者弹出的输入法遮挡底部内容(原因为adjustResize失效 底部区域不会收起)
 * (Description)
 *
 * @author mulinrui
 */
public class KeyboardUtil {
    private View decorView;
    private View contentView;
    private float initialDpDiff = -1;

    private static KeyboardUtil sInstance;

    private KeyboardUtil() {

    }

    public static KeyboardUtil getInstance() {
        if (null == sInstance) {
            sInstance = new KeyboardUtil();
        }
        return sInstance;
    }

    public void enable(View contentView, MarketBaseActivity act) {
        if (VersionUtils.hasKitKat() && contentView != null && act != null) {
            this.decorView = act.getWindow().getDecorView();
            this.contentView = contentView;
            this.initialDpDiff = -1;
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void disable() {
        if (VersionUtils.hasKitKat() && decorView != null) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    // ==========================================================================
    // Inner/Nested Classes
    // ==========================================================================
    // a small helper to allow showing the editText focus
    ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            // r will be populated with the coordinates of your view that area still visible.
            decorView.getWindowVisibleDisplayFrame(r);
            int usableHeightSansKeyboard = decorView.getRootView().getHeight();
            // get the height diff as dp
            float heightDiffDp = usableHeightSansKeyboard - (r.bottom - r.top);
            // set the initialDpDiff at the beginning. (on my phone this was 73dp)
            if (initialDpDiff == -1 && heightDiffDp <= usableHeightSansKeyboard / 4) {
                /**
                 * 一言难尽
                 * initialDpDiff的本质其实就是状态栏的高度且在为-1的时候才赋值。但在华为系统手机上发现切home再回来后。heightDiffDp首次的计算不正确
                 * 会被赋上错误的值。索性就直接调用getStatusBarHeight给initialDpDiff赋值算了。但getStatusBarHeight的值来源是反射的系统的常量值。
                 * 这样三星设备又不行。唉~。只能采用下面的方式。谁值小用谁。三星设备heightDiffDp为0.切Home后回来用statusBarHeight。
                 * liubin 2017/5/18 19:52
                 */
                initialDpDiff = heightDiffDp;
            }

     //要添加的高度
            float bootomPadding = heightDiffDp - initialDpDiff;
            // LogUtils.w(" OnGlobalLayoutListener  bootomPadding:" + bootomPadding);
            // if it could be a keyboard add the padding to the view 此处需要多进行测试
            if (bootomPadding > usableHeightSansKeyboard / 4) { // if more than 100 dp, its probably a keyboard...
                // check if the padding is 0 (if yes set the padding for the keyboard)
                //魅族手机输入内容的时间高度会再次变化 因此加入bootomPadding != contentView.getPaddingBottom()判断
                if (contentView.getPaddingBottom() == 0 || bootomPadding != contentView.getPaddingBottom()) {
                    // set the padding of the contentView for the keyboard
                    contentView.setPadding(0, 0, 0, (int) bootomPadding);
                }
            } else {
                // check if the padding is != 0 (if yes reset the padding)
                if (contentView.getPaddingBottom() != 0) {
                    // reset the padding of the contentView
                    contentView.setPadding(0, 0, 0, 0);
                }
            }
        }
    };
}

相关文章

网友评论

      本文标题: 解决当activity全屏时 使用adjustResize失

      本文链接:https://www.haomeiwen.com/subject/eqvzuxtx.html