关于Android中软键盘显示隐藏的监听判断总结

Android AUmaWSsSoi 2年前 (2017-09-12) 344次浏览 0个评论
文章目录[隐藏]

前言

很多时候我们需要知道软键盘是否打开和关闭,从而处理一些需求。之前在做shou app的时候,由于交互比较复杂,需要知道软键盘是开启或者关闭,决定是否让视频全屏,还是让聊天室全屏,或者是正常显示(上面是视频,下面是聊天室)

ViewTreeObserver.OnGlobalLayoutListener

OnGlobalLayoutListener 是ViewTreeObserver的内部类,当一个视图树的布局发生改变时,可以被ViewTreeObserver监听到,这是一个注册监听视图树的观察者(observer),在视图树的全局事件改变时得到通知。ViewTreeObserver不能直接实例化,而是通过getViewTreeObserver()获得。
以上是说明,摘抄自http://www.cnblogs.com/superle/p/4567400.html这篇文章,也就是说,通过这个api,我们可以知道view的布局状态变化

如何监听软键盘

KeyboardChangeListener github上有这样的一个轮子,测试了下,的确有效,思路是,对android.R.id.content所对应的view设置监听,监听它的高度变化,计算比较,来确定软件盘是否弹起或者收拢了。这种模式需要设置activity
android:windowSoftInputMode=”adjustResize”

full screen模式下如何监听软件盘

在full screen 状态上,发现,以上的方案完全不起作用了。在https://pspdfkit.com/blog/2016/keyboard-handling-on-android/ 上找到了解决办法。
大致上说的是full screen模式下,adjustResize不会生效了,window不会resize的。
getWindowVisibleDisplayFrame(Rect)这个api可以获取到view所在的区域,通过它比较每次区域的变化,可以实现监听软键盘是否可见
完整代码为:

public class KeyBoardChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
    private static final String TAG = "KeyBoardChangeListener";
    // Threshold for minimal keyboard height.
    final int MIN_KEYBOARD_HEIGHT_PX = 150;
    private final Rect windowVisibleDisplayFrame = new Rect();
    // Top-level window decor view.
    private View decorView;
    private int lastVisibleDecorViewHeight;
    private KeyBoardListener keyBoardListener;

    KeyBoardChangeListener(Activity activity) {
        if (activity == null) {
            Log.e(TAG, "activity is null");
            return;
        }
        decorView = activity.getWindow().getDecorView();
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    public void setKeyBoardListener(KeyBoardListener keyBoardListener) {
        this.keyBoardListener = keyBoardListener;
    }

    @Override
    public void onGlobalLayout() {
        // Retrieve visible rectangle inside window.
        decorView.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame);
        final int visibleDecorViewHeight = windowVisibleDisplayFrame.height();
        // Decide whether keyboard is visible from changing decor view height.
        if (lastVisibleDecorViewHeight != 0) {
            if (lastVisibleDecorViewHeight > visibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX) {
                // Calculate current keyboard height (this includes also navigation bar height when in fullscreen mode).
                int currentKeyboardHeight = decorView.getHeight() - windowVisibleDisplayFrame.bottom;
                // Notify listener about keyboard being shown.
                if (keyBoardListener != null) {
                    keyBoardListener.onKeyboardChange(true, currentKeyboardHeight);
                }
            } else if (lastVisibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX < visibleDecorViewHeight) {
                // Notify listener about keyboard being hidden.
                if (keyBoardListener != null) {
                    keyBoardListener.onKeyboardChange(false, 0);
                }
            }
        }
        // Save current decor view height for the next call.
        lastVisibleDecorViewHeight = visibleDecorViewHeight;
    }

    public interface KeyBoardListener {
        /**
         * call back
         *
         * @param isShow         true is show else hidden
         * @param keyboardHeight keyboard height
         */
        void onKeyboardChange(boolean isShow, int keyboardHeight);
    }
}

相关参考

https://pspdfkit.com/blog/2016/keyboard-handling-on-android/
KeyboardChangeListener
https://gist.github.com/xingstarx/7a535fdda432e7678b53c628099c96ab

原文始发于:关于Android中软键盘显示隐藏的监听判断总结


java问与答 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:http://javaexception.com/archives/10
喜欢 (2)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址