我正在开发一个带有角度的应用程序,每当我将输入集中在屏幕底部的输入时,键盘都会重叠。
我尝试将其添加到我的Mobile-config.js,但不起作用:
App.setPreference('fullscreen', false);
App.setPreference('android-windowSoftInputMode', 'adjustResize');
以及我的index.html上的meta:
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, target-densitydpi=device-dpi, width=device-width" />
我忘记了什么吗?
因此,您有两个选择Android(iOS可以对您处理更好(。在您的AndroidManifest.xml
文件中,您会在第一个<activity>
标签内看到android:windowSoftInputMode
。这将调整键盘与屏幕相互作用的方式。这是有关其工作原理的更多信息。
这几乎在所有情况下都对我有用。
此代码在路径下:
── client
├── main.js
// Global variables
let keyboardHeight = 0, originalHeight = 0;
Meteor.startup(() => {
if(Meteor.isCordova){
StatusBar.hide();
// ionic plugin defaults to hide it
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(false);
// Android specific events
isAndroid = cordova.platformId == 'android';
if(isAndroid){
// Handle android backbutton
document.addEventListener("backbutton", onBackButtonDown, false);
// Using ionic-plugin-keyboard
window.addEventListener("native.keyboardshow", onShowKeyboard, false);
window.addEventListener("native.keyboardhide", onHideKeyboard, false);
}
}
};
onShowKeyboard = function(e){
let elem = document.activeElement, // Get the focused element
$parent = $(elem).scrollParent(); // Get closest scrollable ancestor (jQuery UI)
// If there is no scrollable parent, no need to continue processing
if($parent.length == 0){
return;
}
// Check if the keyborad type has changed (i.e. from text to number)
if(keyboardHeight != e.keyboardHeight){
keyboardHeight = e.keyboardHeight;
}
// Don't resize if the original height hasn't been reset by onHideKeyboard()
if(originalHeight == 0){
originalHeight = $parent.height();
}
// Subtract keyboard height from parent height + accessory bar height
// Add some class to the parent, to be able to get it back to normal state onHideKeyboard()
$parent.height(originalHeight - keyboardHeight + 50).addClass('adjusted');
// Give the keyboard time to show
setTimeout(function() {
// Scroll to active element
document.activeElement.scrollIntoView({
behavior: "smooth", // or "auto" or "instant"
block: "center" // or "start" or "end"
});
}, 100);
// Unbind DOM object from HTML for garbage collection
elem = null;
$parent.prevObject = null; // To avoid memory leak (for any jQuery DOM object)
$parent = null;
};
onHideKeyboard = function(e){
let s = $('.adjusted').attr('style');
s = s.replace(/height.*;/, '');
$('.adjusted').attr('style', s).removeClass('adjusted');
keyboardHeight = 0;
originalHeight = 0;
};