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

标签 javascript ios cordova

我正在尝试根据 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/

相关文章:

javascript - 将链接 href 与菜单状态的地址路径进行比较

ios - UITableView 单元格弹出菜单

ios - swift 中的 Objective C 代码是什么?

android - 将 facebook SDK 导入 cordova/phonegap 项目

javascript - ionic 条形码扫描器时间戳集成

javascript - 如何在 jquery 中启用和禁用下拉值

javascript - 在具有不同域 url 的页面内使用 iframe 时,Internet Explorer 中的访问被拒绝

JavaScript 链接点击计数器

iOS Metal - 渲染一个完整的三角形

css - Phonegap 使用 pt 而不是 px