Android检测软键盘关闭 [英] Android detect soft keyboard closing

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

问题描述

我在这个问题上苦苦挣扎了两个小时,无法找到确定是否显示键盘的方法.

我已经看到了关于此问题的多个问题和答案,这取决于容器布局高度的变化.因此不可能(例如)在Edittext获得焦点时准确地检测到键盘的打开,结果,全高布局的改变和它的视图是不可避免的.

I am struggling with this matter for a couple of hours and I cannot find a way to determine if the keyboard is shown or not.

I have seen multiple questions and answer about this matter which was depended on changing in height of container layout. so it cannot be possible to detect the opening of keyboard exactly when (for example) an Edittext get the focused and as a result, change in full height layout and its views is inevitable.

解决方案1: 我通过从所有 EditText 中移开焦点并更改视图的可见性来解决此问题,当Edittext成为焦点时,不需要显示这些视图(请留出可用空间以防止视图混乱)所以我有足够的时间删除多余的视图.

Solution 1 : I solved this problem by removing focus from all of EditText s and change the visibility of the views which I don't need them to be shown when Edittext get focused (make available space to prevent disorder of views) so I have enough time to remove redundant views.

但是无法检测到何时要求关闭键盘以使视图可见.

but it is not possible to detect when the keyboard is asked to be closed to make views visible.

如果我使用第二段中提到的常用方法来检测键盘的关闭,则会导致解决方案1"失败.

if I use the common method which I mentioned in 2nd paragraph to detect closing of the keyboard it will fail my "solution 1".

我现在唯一的想法是通过监视负责键盘关闭的键来检测键盘关闭.机器人,我无法在onKeyDown方法中检测到它.

the only idea I have now is to detect keyboard closing by monitoring the key which is responsible for keyboard closure. bot i cannot detect it in onKeyDown method.

那我怎么监视这个钥匙?

so how can I monitor this key?

任何指导和想法将不胜感激. 预先感谢.

any guide and idea will be appreciated. Thanks in advance.

最终解决方案:
感谢@Nick Cardoso,我的最终解决方案是结合使用onTouchListener检测键盘打开程度和Nick的关闭解决方案.

Final Solution :
thanks to @Nick Cardoso , my final solution is a combination of using onTouchListener to detect the keyboard openness and Nick's solution of closing.

这是用于Edittexts的onTouchListener:

this is onTouchListener for Edittexts :

    View.OnTouchListener TouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            Container.getViewTreeObserver().removeOnGlobalLayoutListener(gll);
            recyclerView.setVisibility(View.GONE);
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Container.getViewTreeObserver().addOnGlobalLayoutListener(gll);
                }
            }, 100);
        }
        return false;
    }
};


ViewTreeObserver.OnGlobalLayoutListener gll = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Rect measureRect = new Rect();
        Container.getWindowVisibleDisplayFrame(measureRect);
        int keypadHeight = Container.getRootView().getHeight() - measureRect.bottom;

        if (keypadHeight > 0) {
            // keyboard is opened
        } else {
            recyclerView.setVisibility(View.VISIBLE);
        }
    }
};

此处Container是布局的根视图.

推荐答案

(荒谬地)没有简单,可靠的方法来做到这一点.但是,可以通过此处建议的布局侦听器分为两部分来实现.有点hack,但是可以用:

There is (ridiculously) no simple, reliable way to do this. It can however be achieved in two parts, with a layout listener as suggested here. It's a little bit of a hack but it works:

mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Rect measureRect = new Rect(); //you should cache this, onGlobalLayout can get called often
        mRootView.getWindowVisibleDisplayFrame(measureRect);
        // measureRect.bottom is the position above soft keypad
        int keypadHeight = mRootView.getRootView().getHeight() - measureRect.bottom;

        if (keypadHeight > 0) {
            // keyboard is opened 
            mIsKeyboardVisible = true;
            mMyDependentView.setVisibility(View.GONE);
        } else {
            //store keyboard state to use in onBackPress if you need to
            mIsKeyboardVisible = false;
            mMyDependentView.setVisibility(View.VISIBLE);
        }
    }
});

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

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