[英]Prevent Keyboard from closing
I am struggling a little bit with this implementation. 我对这个实现有点挣扎。 I'm building my first Hello World!
我正在构建我的第一个Hello World! android(cordova) application that requires a keyboard to show always and avoid hiding it like when the user clicks the back button or any other input.
android(cordova)应用程序,需要键盘始终显示并避免隐藏它,就像用户单击后退按钮或任何其他输入时一样。
Why? 为什么? basically I don't have any input element in my HTML to trigger a focus & show the keyboard, it's kind of a 'terminal emulator' where the user performs certain commands.
基本上我的HTML中没有任何输入元素来触发焦点并显示键盘,它是用户执行某些命令的“终端模拟器”。
The keyboard wasn't showing at all so I went and I added the following: 键盘根本没有显示所以我去了,我添加了以下内容:
Installed Ionic Keyboard plugin 安装了Ionic Keyboard插件
cordova plugin add https://github.com/driftyco/ionic-plugins-keyboard.git
Added a Permission to config.xml 添加了config.xml的权限
<feature name="Keyboard">
<param name="android-package" value="com.ionic.keyboard.IonicKeyboard" />
<param name="onload" value="true" />
</feature>
In my App module, the following lines : 在我的App模块中,以下行:
var myApp = angular.module('myApp', ['ionic']);
myApp.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
window.cordova.plugins.Keyboard.show(); // Show Keyboard on startup
// and here Trigger a show keyboard when hidden
window.addEventListener('native.hidekeyboard', keyboardHideHandler);
function keyboardHideHandler(e){
window.cordova.plugins.Keyboard.show();
}
}
});
});
Now, the above implementation works but I do not think it is elegant to handle it this way and I don't like the feel that the keyboard closes then pops up again. 现在,上面的实现工作,但我不认为这样处理它是优雅的,我不喜欢键盘关闭然后再次弹出的感觉。
Hope I am on the right track.Any hints would be appreciated. 希望我走在正确的轨道上。任何提示都将受到赞赏。
Thank you 谢谢
Screenshots 截图
EDIT: I think the standard way to do it is here: https://stackoverflow.com/a/1510005/1091751 . 编辑:我认为这样做的标准方法是: https : //stackoverflow.com/a/1510005/1091751 。 This won't prevent it from closing when the backbutton is pressed however, you could try editing the actual Cordova Android files in
platforms/android
to override the following method (taken from https://stackoverflow.com/a/6571093/1091751 ): 当按下后退按钮时,这不会阻止它关闭,但是,您可以尝试在
platforms/android
编辑实际的Cordova Android文件以覆盖以下方法(取自https://stackoverflow.com/a/6571093/1091751 ) :
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
InputMethodManager manager = (InputMethodManager) this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
manager.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
}
return false;
}
I haven't tested this out, but what if you add a hidden input that you focus initially when your app loads and then constantly refocus it when it loses focus? 我没有测试过这个,但是如果你添加一个隐藏的输入,当你的应用加载时你最初关注它,然后在它失去焦点时不断重新聚焦,该怎么办? I'm not sure this will look any different than explicitly calling keyboard.show(), but it might prevent the keyboard opening/closing jitter.
我不确定这与显式调用keyboard.show()有什么不同,但它可能会阻止键盘打开/关闭抖动。
Something like: 就像是:
<input constant-focus id="hiddenFocus" type="hidden">
and then 然后
document.getElementById('hiddenFocus').focus()
then constantly refocus it to keep the keyboard up: // HTML tag = constant-focus
然后不断重新调整它以保持键盘:// HTML标签=
constant-focus
.directive('constantFocus', function(){
return {
restrict: 'A',
link: function(scope, element, attrs){
element[0].addEventListener('focusout', function(e){
element[0].focus();
});
}
};
})
Well, I think I came up with a different way, still not sure if this is how it should be handled. 好吧,我想我想出了一个不同的方式,仍然不确定这是否应该如何处理。
Add an event listener on Taps 在Taps上添加一个事件监听器
myApp.directive('detectGestures', function ($ionicGesture) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
var gestureType = attrs.gestureType;
switch (gestureType) {
case 'doubletap':
$ionicGesture.on('doubletap', scope.reportEvent, elem);
break;
}}}
});
Then in My Controller, If keyboard is Visible close else Show 然后在我的控制器中,如果键盘是可见的,则关闭其他显示
$scope.reportEvent = function (event) {
if (event.type == 'doubletap') {
$timeout(function () {
if (window.cordova && window.cordova.plugins.Keyboard) {
if(cordova.plugins.Keyboard.isVisible){
window.cordova.plugins.Keyboard.close();
} else {
window.cordova.plugins.Keyboard.show();
}
}
}, 500);
}
};
Let me know what you think. 让我知道你的想法。
Thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.