JavaEar 专注于收集分享传播有价值的技术资料

使用导航栏关闭按钮检测软键盘关闭 (Detect soft keyboard closing by use of nav bar close button)

I've spent most of the day searching and trying various solutions and while I've come most of the way, I'm cursed by the "close" (maybe it has more appropriate name, idk) button on the nav bar when the keyboard is displayed (as in the attached image.)

I have a few editTexts which allow the user to adjust some parameters before a graph is recomputed and redrawn. I need to know when their input is complete. I've managed to sort out the "Done" button but for the life of me I can't figure out how to handle hitting that close button. I've also adapted some code which determines if the keyboard was opened and then closed (which is the case with these editTexts) but it only works when using the Done button (so is somewhat redundant).

So.. is there some way of picking up when the user has closed the keyboard using the nav bar?

TIA

*@! keyboard

1个回答

    最佳答案

  1. 英文原文

    Well, I think I have something here, perhaps it will be useful to others. Who knows how long it will work properly.

    So prelude: I needed to be able to tell when a user had completed changing an edit text so that I could then redo a computation and update a graph. I really have no room for an "update" button and felt from a UI standpoint it would have quickly become annoying.

    Codewise:

    I have a "display" method with an inner "watcher" class with a watch() method. The watcher class has many things, including the various editTexts which watch()..watches. I ddapted some code I found here, which determines if the keyboard is open or closed. currentView is the root view of the graphing activity.

                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;
                            }
                        }
                    }
                });
    

    This helps but in a somewhat backward way. Knowing open/closed by itself is not useful within any of the text watchers as those are not triggered by the act of closing the keyboard (so you can't actually test for it within the listener)

    However, thinking about it a bit I added a boolean keyboard toggle which is set to true in the afterTextChanged of the addTextChangedListener. In addition, I put the rID of that editText in another class variable.

    The code which checks the keyboard status then calls the appropriate updating procedure if the a) keyboard is closed b) some text has actually been changed (keyboard toggle) and c) uses the rID to determine what procedure to then call.

    I know its a bit convoluted, but it does seem to work.


    中文翻译

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

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

    Codewise:

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

      currentView.getViewTreeObserver()。addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
    
                    @覆盖
                    public void onGlobalLayout(){
                        // TODO自动生成的方法存根
                        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){//有人更新了其中一个editTexts
                            if(!keyboardVisible){//他们关闭了软键盘就这样完成了
                                // 做一点事
                                if(MainActivity.debug){
                                    if(rIDchangedET == firstET.getId())Log.i(TAG," 更新第一件事" );
                                    if(rIDchangedET == secondET.getId())Log.i(TAG," 更新第二件事" );
                                    if(rIDchangedET == thirdET.getId())Log.i(TAG," 更新第三件事" );
                                }
                                //重置键盘切换
                                keyboardToggle = false;
                            }
                        }
                    }
                });
     

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

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

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

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

    Well, I think I have something here, perhaps it will be useful to others. Who knows how long it will work properly.

    So prelude: I needed to be able to tell when a user had completed changing an edit text so that I could then redo a computation and update a graph. I really have no room for an "update" button and felt from a UI standpoint it would have quickly become annoying.

    Codewise:

    I have a "display" method with an inner "watcher" class with a watch() method. The watcher class has many things, including the various editTexts which watch()..watches. I ddapted some code I found here, which determines if the keyboard is open or closed. currentView is the root view of the graphing activity.

                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;
                            }
                        }
                    }
                });
    

    This helps but in a somewhat backward way. Knowing open/closed by itself is not useful within any of the text watchers as those are not triggered by the act of closing the keyboard (so you can't actually test for it within the listener)

    However, thinking about it a bit I added a boolean keyboard toggle which is set to true in the afterTextChanged of the addTextChangedListener. In addition, I put the rID of that editText in another class variable.

    The code which checks the keyboard status then calls the appropriate updating procedure if the a) keyboard is closed b) some text has actually been changed (keyboard toggle) and c) uses the rID to determine what procedure to then call.

    I know its a bit convoluted, but it does seem to work.

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

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

    Codewise:

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

      currentView.getViewTreeObserver()。addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
    
                    @覆盖
                    public void onGlobalLayout(){
                        // TODO自动生成的方法存根
                        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){//有人更新了其中一个editTexts
                            if(!keyboardVisible){//他们关闭了软键盘就这样完成了
                                // 做一点事
                                if(MainActivity.debug){
                                    if(rIDchangedET == firstET.getId())Log.i(TAG," 更新第一件事" );
                                    if(rIDchangedET == secondET.getId())Log.i(TAG," 更新第二件事" );
                                    if(rIDchangedET == thirdET.getId())Log.i(TAG," 更新第三件事" );
                                }
                                //重置键盘切换
                                keyboardToggle = false;
                            }
                        }
                    }
                });
     

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

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

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

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