监听键盘弹出事件

工具类

public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {
	
    public interface SoftKeyboardStateListener {
        void onSoftKeyboardOpened(int keyboardHeightInPx);
        void onSoftKeyboardClosed();
    }
    
    private static final int KEYBOARD_HEIGHT = DensityUtil.dip2px(context, 100);
    private final List<SoftKeyboardStateListener> listeners = new LinkedList<>();
    private final View activityRootView;
    private int lastSoftKeyboardHeightInPx;
    private boolean isSoftKeyboardOpened;
    public SoftKeyboardStateHelper(View activityRootView) {
        this(activityRootView, false);
    }
    public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) {
        this.activityRootView = activityRootView;
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }
    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        activityRootView.getWindowVisibleDisplayFrame(r);
        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        if (!isSoftKeyboardOpened
                && heightDiff
                        > KEYBOARD_HEIGHT) { // if more than 100 pixels, its probably a keyboard...
            isSoftKeyboardOpened = true;
            notifyOnSoftKeyboardOpened(heightDiff);
        } else if (isSoftKeyboardOpened && heightDiff < KEYBOARD_HEIGHT) {
            isSoftKeyboardOpened = false;
            notifyOnSoftKeyboardClosed();
        }
    }
    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
    }
    public boolean isSoftKeyboardOpened() {
        return isSoftKeyboardOpened;
    }
    public int getLastSoftKeyboardHeightInPx() {
        return lastSoftKeyboardHeightInPx;
    }
    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.add(listener);
    }
    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.remove(listener);
    }
    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;

        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardOpened(keyboardHeightInPx);
            }
        }
    }
    private void notifyOnSoftKeyboardClosed() {
        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardClosed();
            }
        }
    }
}

使用:

	SoftKeyboardStateHelper softKeyboardStateHelper =
			new SoftKeyboardStateHelper(findViewById(R.id.content_wrapper));      //最外层的layout
	        softKeyboardStateHelper.addSoftKeyboardStateListener(
                new SoftKeyboardStateHelper.SoftKeyboardStateListener() {
                    @Override
                    public void onSoftKeyboardOpened(int keyboardHeightInPx) {
                    }
                    @Override
                    public void onSoftKeyboardClosed() {
                    }
                });
    }

windowSoftInputMode = adjustNothing 时该控件不会生效
windowSoftInputMode = adjustPan 保证获得焦点的控件不被遮挡
windowSoftInputMode = adjustResize 在RelativeLayout或FragmentLayout中没有从上到下的强制约束和话(比如上半部分从上到下布局,下半部分从下到上布局)这样使用该属性就会压缩布局大小,直接将下半部分的控件上推至不被键盘遮挡,上半部分会被压缩.

另华为安手机的安全键盘:在用户名和密码输入框之间相互切换时会自动切换输入法,导致打开键盘和关闭键盘在不当的时机多次调用;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值