【发布时间】:2019-12-28 18:51:18
【问题描述】:
我正在尝试根据 html 输入类型 text 、 tel 或 email 更改我的键盘类型。
我已经设法使用cordova插件更改键盘类型,这是代码。
NSString* UIClassString = [@[@"UI", @"Web", @"Browser", @"View"] componentsJoinedByString:@""];
NSString* UITraitsClassString = [@[@"UI", @"Text", @"Input", @"Traits"] componentsJoinedByString:@""];
IMP newImp = imp_implementationWithBlock(^(id _s) {
return UIKeyboardTypeASCIICapable;
// return UIKeyboardTypeDefault;
});
for (NSString* classString in @[UIClassString, UITraitsClassString]) {
Class c = NSClassFromString(classString);
Method m = class_getInstanceMethod(c, @selector(keyboardType));
if (m != NULL) {
method_setImplementation(m, newImp);
} else {
class_addMethod(c, @selector(keyboardType), newImp, "l@:");
}
}
通过使用 iOS 的 UIKeyboardWillShowNotification 管理检测导致键盘显示的元素并添加使用 document.activeElement 获取活动元素的事件侦听器
下面是代码
本地人:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(onKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
- (void)onKeyboardWillShow:(NSNotification *)note{
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('keyboardWillShow', null, true);"];
}
Javascript:
document.addEventListener('keyboardWillShow', function() {
let element = document.activeElement
let type = _.get(element,'type')
if(_.isEqual(type,'text')){
changeKeyboardType('text')
}else if(_.isEqual(type,'tel')){
changeKeyboardType('tel')
}
}, false);
根据编写的解决方案,键盘在键盘更改发生之前显示,所以问题是。
是否有任何用于 javascript 的“键盘显示前”事件监听器?
【问题讨论】:
-
参考这个 stackoverflow 答案 - stackoverflow.com/a/8242457/6536335
-
这可能也有帮助 - stackoverflow.com/a/24303517/6536335
-
我会先尝试第一个解决方案,希望可行,因为在此之前我确实尝试过 document.addEventListener('click'),但键盘在更改之前会打开。
-
焦点事件以异步方式调用。键盘仍然显示。
标签: javascript ios cordova