gpt4 book ai didi

javascript - Cordova 应用程序 - 键盘显示/打开之前的事件监听器

转载 作者:行者123 更新时间:2023-11-29 05:30:38 25 4
gpt4 key购买 nike

我正在尝试根据 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@:");
}
}

关于javascript - Cordova 应用程序 - 键盘显示/打开之前的事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57626406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com