Android 监听软键盘显示和隐藏

1. 监听软键盘

由于官方没有提供相关的监听,只能通过界面布局来判断软键盘显示和隐藏。

通过OnLayoutChangeListener来监听

getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom,
                int oldLeft, int oldTop, int oldRight, int oldBottom) {

        }
    });

通过OnGlobalLayoutListener来监听

getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

        }
    });

判断软键盘显示和隐藏。

private boolean isShowing() {
    // 获取当前屏幕内容的高度
    int screenHeight = getWindow().getDecorView().getHeight();

    // 获取View可见区域的bottom
    Rect rect = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

    return screenHeight > rect.bottom;
}

不过在某些导航栏存在的手机上会发生某些问题,可以通过获取导航栏高度来避免。

private int getNavigatorBarHeight() {
    int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
    int height = getResources().getDimensionPixelSize(resourceId);

    LogTool.logi("SoftManagerListenerActivity", "Status Bar Height = " + height);

    return height;
}

2. 软键盘显示

软键盘显示时可能会覆盖某些控件,必要时需要移动界面,同时华为的有些手机型号支持底部导航栏弹出收起操作。

private void onLayout() {
    //获取当前屏幕内容的高度
    int screenHeight = getWindow().getDecorView().getHeight();

    //获取View可见区域的bottom
    Rect rect = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

    int visibleHeight = rect.height()
    // 设置200是为了防止底部导航栏弹出收起而引起的误判
    if (screenHeight - rect.bottom > 200) {
        // 防止重复
        if (!mShowSoftKeyBoard) {
            mShowSoftKeyBoard = true

            ... ...
        } else {
            // diff大于0, 底部导航栏消失,diff小于0,底部导航栏弹出
            var diff = visibleHeight - mVisibleHeight
            if (diff < 0) {
                mBottomHeight = -diff
            } else if (diff > 0){
                mBottomHeight = 0
            }
        }
    } else {
        // 键盘消失
        if (visibleHeight - mVisibleHeight > 200) {
            mShowSoftKeyBoard = false
        } else {
            int bottomHeight = screenHeight - rect.bottom
            int diff = bottomHeight - mBottomHeight
            if (diff != 0) {
                // diff大于0, 底部导航栏弹出,diff小于0,底部导航栏消失
                ... ...
            }
            mBottomHeight = bottomHeight
        }
    }
    mVisibleHeight = visibleHeight
}

效果如下
在这里插入图片描述

3. 手动显示和隐藏键盘

有些手机在进入界面时不能自动弹出键盘,为确保体验体验,可以代码中弹出。调用postDelayed方法,是为确保activity创建完毕。

EditText editText = findViewById(R.id.edit_text);
InputMethodManager manager =  (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable() {
    @Override
    public void run() {
        manager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }
}, 300);

隐藏键盘

manager.hideSoftInputFromInputMethod(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

参考资料:https://www.cnblogs.com/shelly-li/p/5639833.html
参考资料:http://blog.csdn.net/sinat_31311947/article/details/53914000

相关文章
Android TextView控件
Android Span应用
Android EditText控件
Android 监听软键盘显示和隐藏

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值