Prevent Keyboard from closing

后端 未结 2 1690
傲寒
傲寒 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:33

    Well, I think I came up with a different way, still not sure if this is how it should be handled.

    Add an event listener on Taps

    myApp.directive('detectGestures', function ($ionicGesture) {
      return {
        restrict: 'A',
        link: function (scope, elem, attrs) {
            var gestureType = attrs.gestureType;
            switch (gestureType) {
                case 'doubletap':
                    $ionicGesture.on('doubletap', scope.reportEvent, elem);
                    break;
        }}}
    });
    

    Then in My Controller, If keyboard is Visible close else Show

    $scope.reportEvent = function (event) {
          if (event.type == 'doubletap') {
              $timeout(function () {
                  if (window.cordova && window.cordova.plugins.Keyboard) {
                      if(cordova.plugins.Keyboard.isVisible){
                          window.cordova.plugins.Keyboard.close();
                      } else {
                          window.cordova.plugins.Keyboard.show();
                      }
    
                  }
               }, 500);
             }
          };
    

    Let me know what you think.
    Thanks!

    0 讨论(0)
  • 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:

    <input constant-focus id="hiddenFocus" type="hidden">
    

    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();
              });
            }
          };
        })
    
    0 讨论(0)
提交回复
热议问题