Detecting android softkeyboard show/hide events

前端 未结 2 1643
既然无缘
既然无缘 2020-12-15 08:11

I am trying to detect showKeyboard and hidekeyboard events on phonegap. For that purpose, on deviceready event I placed following code

相关标签:
2条回答
  • 2020-12-15 08:21

    Using ionic-plugin-keyboard, the events native.keyboardshowand native.keyboardhide are fired, even in fullscreen / immersive Mode:

    Example Code:

    document.addEventListener('deviceready', 
      function(){
        // disable immersive mode  on Android when keyboard is shown
            try {
          if (cordova.platformId == 'android') {
            AndroidFullScreen.immersiveMode(false, false);
            window.addEventListener('native.keyboardshow', function (e) {
              AndroidFullScreen.showSystemUI(false, false);
    
            });
            window.addEventListener('native.keyboardhide', function (e) {
              AndroidFullScreen.immersiveMode(false, false);
            });
          }
        } catch (error) {
          console.log('deviceready - ' + error);
        }
    }, false);
    
    0 讨论(0)
  • 2020-12-15 08:36

    Apparently showkeyboard / hidekeyboard events WILL NOT fire when you are running your app in a full-screen mode (eg. no status bar at the top) because the screen size doesn't change when the keyboard pops-up.
    https://issues.apache.org/jira/browse/CB-392

    If you do not want your app to run in fullscreen :

    Try this first let's see if it narrows down your problem to the event firing or the function it tries to call when the event fires.

    function onDeviceReady() {
        alert("Device Ready");
        document.addEventListener("showkeyboard", function(){ alert("Keyboard is ON");}, false);
        document.addEventListener("hidekeyboard", function(){ alert("Keyboard is OFF");}, false);
    }
    

    I have tested this on Android 4.2 and 4.3 worked fine on both.

    Note:

    To turn fullscreen off:
    Remove NoTitleBar from your AndroidManifest.xml:

    android:theme="@android:style/Theme.Black.NoTitleBar
    

    And / or add these lines to the onCreate method in your MainActivity.java:

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    
    0 讨论(0)
提交回复
热议问题