【问题标题】:Detecting android softkeyboard show/hide events检测android软键盘显示/隐藏事件
【发布时间】:2013-11-19 21:01:11
【问题描述】:

我正在尝试检测 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);
},

这里backbuttonevent 被触发并且工作正常,但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);
}

【问题讨论】:

  • 没有隐藏和显示键盘之类的事件(docs.phonegap.com/en/edge/cordova_events_events.md.html#Events
  • 如果你想检测键盘的状态,你需要编写一个自定义的phonegap插件,然后使用Java检测键盘是向上还是向下,然后将它传递给你的Javascript
  • @Ard 那么我是否也需要为 iOS 编写类似的插件?
  • @Ard 没有这样的插件吗?
  • 键盘也没有改变屏幕高度,它只是重叠了它。如果它会改变屏幕高度,那么我可能能够检测到并相应地做一些事情。那么 android manifest 中是否有任何这样的配置,以便键盘调整窗口大小而不是仅仅重叠它?

标签: cordova dom-events android-softkeyboard cordova-3 android-event


【解决方案1】:

显然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 上对此进行了测试。

注意:

关闭全屏:
从您的AndroidManifest.xml 中删除NoTitleBar

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

和/或将这些行添加到 MainActivity.java 中的 onCreate 方法中:

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

【讨论】:

  • 这是正确的;全屏是指Android上的全屏。您的窗口不会针对状态栏缩小,也不会针对键盘缩小。 Phonegap 依靠其窗口缩小来检测键盘的存在,因此检测将无法在全屏模式下进行。另请注意,即使绝大多数键盘在非全屏时确实会导致 Phonegap 窗口缩小,但如果您的用户使用更非正统的键盘应用程序,您可能仍然无法检测到它。这不是你应该依赖的东西。
  • 这没有显示移动设备的标题栏。这两件事都告诉了上面。
  • @Hafiz 你使用Phonegap 闪屏吗?以我的经验,它与全屏设置混淆。因此,必须在启动画面显示后设置这些避免全屏的标志。
  • 已在启动画面后添加设置。另外我现在添加了我的 oncreate 方法代码,请检查那里的代码。
  • 把所有标志的东西放在super.loadUrl(Config.getStartUrl(), 3000);之后
【解决方案2】:

使用ionic-plugin-keyboard,事件native.keyboardshownative.keyboardhide被触发,即使在全屏/沉浸模式下:

示例代码:

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

【讨论】:

    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多