【问题标题】:Cordova Application - Event Listener for before keyboard show / openCordova 应用程序 - 键盘显示/打开之前的事件侦听器
【发布时间】:2019-12-28 18:51:18
【问题描述】:

我正在尝试根据 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 的“键盘显示前”事件监听器?

【问题讨论】:

  • 参考这个 stackoverflow 答案 - stackoverflow.com/a/8242457/6536335
  • 这可能也有帮助 - stackoverflow.com/a/24303517/6536335
  • 我会先尝试第一个解决方案,希望可行,因为在此之前我确实尝试过 document.addEventListener('click'),但键盘在更改之前会打开。
  • 焦点事件以异步方式调用。键盘仍然显示。

标签: javascript ios cordova


【解决方案1】:

通过触发一些 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@:");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2014-09-10
    相关资源
    最近更新 更多