【发布时间】:2013-11-19 21:01:11
【问题描述】:
我正在尝试检测 phonegap 上的 showKeyboard 和 hidekeyboard 事件。为此,在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 被触发并且工作正常,但hidekeyboard 和showkeyboard 事件从未触发。
为了检测它,我尝试使用在浏览器中工作的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