Prevent Keyboard from closing

后端 未结 2 1689
傲寒
傲寒 2021-01-14 13:45

I am struggling a little bit with this implementation. I\'m building my first Hello World! android(cordova) application that requires a keyboard to show always and avoid hid

2条回答
  •  爱一瞬间的悲伤
    2021-01-14 14:38

    EDIT: I think the standard way to do it is here: https://stackoverflow.com/a/1510005/1091751. This won't prevent it from closing when the backbutton is pressed however, you could try editing the actual Cordova Android files in platforms/android to override the following method (taken from https://stackoverflow.com/a/6571093/1091751):

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            InputMethodManager manager = (InputMethodManager) this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
        }
        return false;
    }
    

    I haven't tested this out, but what if you add a hidden input that you focus initially when your app loads and then constantly refocus it when it loses focus? I'm not sure this will look any different than explicitly calling keyboard.show(), but it might prevent the keyboard opening/closing jitter.

    Something like:

    
    

    and then

    document.getElementById('hiddenFocus').focus()
    

    then constantly refocus it to keep the keyboard up: // HTML tag = constant-focus

     .directive('constantFocus', function(){
          return {
            restrict: 'A',
            link: function(scope, element, attrs){
    
              element[0].addEventListener('focusout', function(e){
                element[0].focus();
              });
            }
          };
        })
    

提交回复
热议问题