我正在尝试在phonegap上检测showKeyboardhidekeyboard事件。为此,在deviceready事件中,我放置了以下代码:

  bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
    document.addEventListener("menubutton",app.onMenuKeyPress,false);
    document.addEventListener("backbutton",navigateBack,false);
    document.addEventListener("hidekeyboard", onKeyboardHide, false);
    document.addEventListener("showkeyboard", onKeyboardShow, false);
},

在这里,backbutton事件被触发并且可以正常工作,但是hidekeyboardshowkeyboard事件从不触发。
为了检测它,我尝试使用在浏览器中起作用的window.onresize事件。以下是其代码:
window.onresize = function(){
    var screenHeight = $(window).height();
    alert(screenHeight);
    var diff = screenInitialHeight - screenHeight;
    var newHeight = screenInitialHeight-diff;
    alert(newHeight);
    $('#mainpage').height(newHeight);
    $('#nav_container').height(newHeight);
}

但是此代码也没有在显示或隐藏键盘上执行。此功能仅在首次使用应用程序时执行。开始。我在某些地方看到这些事件对某些人有用,所以我认为我这边有些问题,可能是在某些配置文件中等等。所以下面是androidmanifest.xml代码:
    <?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="1.0.0" android:windowSoftInputMode="adjustPan" package="com.phonegap.move_custom" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:largeHeap="true">
        <activity android:configChanges="keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="move_custom" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
</manifest>

请让我知道是否需要磨碎任何东西。另外,如果这些事件在某人的应用程序中有效,则请共享您的应用程序。这样我就可以尝试检查配置和代码,以了解为什么它无法在我的应用中正常工作。所有的努力将不胜感激。好像我已经在附近但是缺少一些东西。因此,您可以说的任何内容都可能会有所帮助。
    @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    if (getResources().getConfiguration().orientation == 2) {
        super.setIntegerProperty("splashscreen", R.drawable.splash);
    }
    else {
        super.setIntegerProperty("splashscreen", R.drawable.splashportrait);
    }

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    super.loadUrl(Config.getStartUrl(), 3000);
}

最佳答案

当您以全屏模式运行应用程序时(例如,顶部无状态栏),显然showkeyboard / hidekeyboard事件将不会触发,因为弹出键盘时屏幕大小不会改变。
https://issues.apache.org/jira/browse/CB-392
如果您不希望您的应用全屏运行:
首先尝试一下,看看它是否将问题缩小到事件触发或事件触发时尝试调用的函数。

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

我已经在Android 4.2和4.3上对此进行了测试,两者都可以正常工作。
注意:
要关闭全屏显示,请执行以下操作:
NoTitleBar中删除​​AndroidManifest.xml
android:theme="@android:style/Theme.Black.NoTitleBar

和/或将这些行添加到MainActivity.java中的onCreate方法中:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

07-28 01:57
查看更多