使用导航栏关闭按钮检测软键盘关闭

时间:2014-08-26 21:52:47

标签: android keyboard android-edittext navbar

我一天中大部分时间都在寻找和尝试各种解决方案,虽然我已经大部分时间来到这里,但我已经被"关闭"诅咒了。 (显示键盘时,导航栏上可能有更合适的名称,idk)按钮(如附图所示)。

我有一些editTexts,允许用户在重新计算和重绘图形之前调整一些参数。我需要知道他们的输入何时完成。我已经成功地解决了“完成”问题。按钮,但对于我的生活,我无法弄清楚如何处理按下关闭按钮。我还调整了一些代码来确定键盘是否被打开然后关闭(这是editTexts的情况),但只有在使用完成按钮时才有效(因此有些多余)。

那么..当用户使用导航栏关闭键盘时,是否有某种方法可以启动?

TIA

*@! keyboard

1 个答案:

答案 0 :(得分:0)

嗯,我想我在这里有一些东西,也许对别人有用。谁知道它能正常工作多久。

所以前奏:我需要能够告诉用户何时完成了更改编辑文本,以便我可以重做计算并更新图形。我真的没有“更新”按钮的空间,从UI的角度来看,它会很快变得烦人。

Codewise:

我有一个“display”方法,带有一个带有watch()方法的内部“watcher”类。观察者类有很多东西,包括watch().. watch的各种editTexts。我发现了一些我发现here的代码,它确定键盘是打开还是关闭。 currentView是图形活动的根视图。

            currentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    // TODO Auto-generated method stub
                    Rect r = new Rect();
                    getWindow().getDecorView().getWindowVisibleDisplayFrame(r);

                    int screenHeight = getWindow().getDecorView().getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom - r.top);
                    if (MainActivity.debug) Log.d("Keyboard Size", "Size: " + heightDifference+" screenHeight: "+screenHeight);
                    boolean keyboardVisible = heightDifference > (screenHeight / 3);
                    if (keyboardToggle) { // someone has updated one of the editTexts
                        if(!keyboardVisible) { // and they have closed the soft keyboard so are done
                            // do something
                            if(MainActivity.debug) {
                                if(rIDchangedET == firstET.getId()) Log.i(TAG,"update the first thing");
                                if(rIDchangedET == secondET.getId()) Log.i(TAG,"update the second thing");
                                if(rIDchangedET == thirdET.getId()) Log.i(TAG,"update the third thing");
                            }
                            // reset the keyboard toggle
                            keyboardToggle = false;
                        }
                    }
                }
            });

这有点落后。知道打开/关闭本身在任何文本观察者中都没有用,因为那些不是由关闭键盘的行为触发的(因此你无法在听众中实际测试它)

然而,考虑一下我添加了一个布尔键盘切换,在addTextChangedListener的afterTextChanged中设置为true。另外,我将editText的rID放在另一个类变量中。

检查键盘状态的代码然后在a)键盘关闭时调用适当的更新过程b)实际上已经改变了一些文本(键盘切换)和c)使用rID来确定随后调用的过程。

我知道它有点复杂,但似乎确实有效。

相关问题