检测软键盘隐藏状态 [英] Detecting Soft Keyboard Hidden State

查看:70
本文介绍了检测软键盘隐藏状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次软键盘状态从显示更改为隐藏时,都希望输入Toast文本.在这里,我只想从EditText中选择getText(),每次单击EditText时,都必须打开软键盘,然后按回车键或返回后,文本必须显示为Toast

Want to Toast a text everytime when soft keyboard state changes from shown to hidden. Here I just want to getText() from EditText and everytime I click on EditText the soft Keyboard must open and after pressing back or return the text must be shown as Toast

预先感谢

推荐答案

没有用于键盘状态检测的直接侦听器,因此您需要以下一些编程实现

There is no direct listener for keyboard state detection so you need some programatic implementation as below

private boolean wasKeyboardOpen = false;

try {
        activityMainView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                Rect r = new Rect();
                activityMainView.getWindowVisibleDisplayFrame(r);

                int heightDiff = activityMainView.getRootView().getHeight() - (r.bottom - r.top);
                if (heightDiff > 100) {
                    wasKeyboardOpen = true;
                    // kEYBOARD IS OPEN

                } else {
                    if (wasKeyboardOpen) {
                        wasKeyboardOpen = false;
                        // Do your toast here

                    }
                    // kEYBOARD IS HIDDEN
                }
            }
        });
    } catch (Throwable e) {
        e.printStackTrace();
    }

这篇关于检测软键盘隐藏状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆