检测android软键盘显示/隐藏事件 [英] detecting android softkeyboard show/hide events
问题描述
我想在phonegap上检测showKeyboard和hidekeyboard事件。为此,在deviceready事件我放置以下代码:
bindEvents:function(){
document.addEventListener 'deviceready',this.onDeviceReady,false);
},
// deviceready事件处理程序
//
//'this'的范围是事件。为了调用'receivedEvent'
//函数,我们必须显式调用'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事件被触发并且正常工作,但 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);
}
但是这个代码也没有在显示或隐藏键盘上执行。此功能仅在第一次应用程序执行时。开始。我在某些地方看到,对于一些人这些事件是工作,所以我认为有一些事情从我身边,可能在一些配置文件e.t.c.以下是androidmanifest.xml代码:
<?xml version ='1.0'encoding ='utf-8'? ;
< manifest android:hardwareAccelerated =trueandroid:versionCode =1android:versionName =1.0.0android:windowSoftInputMode =adjustPanpackage =com.phonegap.move_customxmlns:android = http://schemas.android.com/apk/res/android\">
< supports-screens android:anyDensity =trueandroid:largeScreens =trueandroid:normalScreens =trueandroid:resizeable =trueandroid:smallScreens =trueandroid:xlargeScreens = />
< uses-permission android:name =android.permission.INTERNET/>
< application android:debuggable =trueandroid:hardwareAccelerated =trueandroid:icon =@ drawable / iconandroid:label =@ string / app_nameandroid:largeHeap =true
< activity android:configChanges =keyboardHidden | keyboard | screenSize | localeandroid:label =@ string / app_nameandroid:name =move_customandroid:screenOrientation =portraitandroid: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.cameraandroid:required =false/>
< uses-sdk android:minSdkVersion =10android: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上测试了这两个。 p>
注意:
要关闭全屏:
删除 NoTitleBar
:
:
:
:
android:theme =@ android:style / Theme.Black.NoTitleBar
将这些行添加到 MainActivity.java 中的 onCreate
方法:
code> getWindow()。clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow()。addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
I am trying to detect showKeyboard and hidekeyboard events on phonegap. For that purpose, on deviceready event I placed following code:
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);
},
Here backbutton event is being fired and working fine but hidekeyboard
and showkeyboard
events are never fired.
Also to detect it I tried to use window.onresize
event, that worked in browser. Following is its code:
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);
}
But this code also didn't executed on show or hide keyboard. This function is only executing when first time app. is started. I saw at some places that for some people these events are working so I think there is some thing wrong from my side, probably in some config file e.t.c. So following is androidmanifest.xml code:
<?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>
Please let me know if anything need to be chagned. Also if these events are working in someone's app, then please share your app. so that I can try to check configuration and code that why it isn't working in my app. All good efforts will be appreciated. Seems like I am already near but missing something. So anything you can tell will probably be helpful.
@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);
}
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);
这篇关于检测android软键盘显示/隐藏事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!