Cordova应用程序-键盘显示/打开之前的事件监听器 [英] Cordova Application - Event Listener for before keyboard show / open

查看:247
本文介绍了Cordova应用程序-键盘显示/打开之前的事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于html输入类型texttelemail更改键盘类型.

I'm trying to change my keyboard type based on html input type text , tel or email.

我设法使用cordova插件更改了键盘类型,这是代码.

I have manage to change the keyboard type using cordova plugin, here is the code.

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

Manage to detect the element that cause the keyboard to show by using iOS's UIKeyboardWillShowNotification and adding an event listener that get active element using 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:

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);

根据编写的解决方案,在更改键盘之前会先显示键盘,这样就可以了.

Based on the solution written, the keyboard is being shown before the keyboard change happens, so the question would be.

是否有用于JavaScript的键盘显示前"事件监听器?

Is there any 'before keyboard show' event listener for javascript?

推荐答案

通过触发一些JavaScript来获取活动元素类型并相应地更改键盘类型,从而设法更改键盘.

Manage to change the keyboard by firing some javascript to get active element type and changes the keyboard type accordingly instead.

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

I can't seems to modify the keyboard type via UIKeyboardWillShowNotification event as it is too late.

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

Here is my code on changing the keyboard type based on active element type,

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@:");
    }
}

这篇关于Cordova应用程序-键盘显示/打开之前的事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆