科尔多瓦应用程序 - 键盘显示/打开之前的事件侦听器



我正在尝试根据 html 输入类型更改我的键盘类型texttelemail.

我已经设法使用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 是否有任何"键盘显示之前"事件侦听器?

设法通过触发一些 javascript 来更改键盘以获取活动元素类型并相应地更改键盘类型。

我似乎无法通过UIKeyboardWillShowNotification事件修改键盘类型,因为为时已晚。

这是我根据活动元素类型更改键盘类型的代码,

NSString* UITraitsClassString = [@[@"UI", @"Text", @"Input", @"Traits"] componentsJoinedByString:@""];
IMP newImp = imp_implementationWithBlock(^(id _s) {
NSString *script = @"document.activeElement.type";
NSString *type = @"";
if ([self.webView isKindOfClass:[UIWebView class]]) {
type = [(UIWebView*)self.webView stringByEvaluatingJavaScriptFromString:script];
}
if([type isEqualToString:@"text"]){
return UIKeyboardTypeASCIICapable;
} else if([type isEqualToString:@"tel"]){
return UIKeyboardTypeASCIICapableNumberPad;
}
return UIKeyboardTypeDefault;
});
for (NSString* classString in @[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@:");
}
}

最新更新