热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android键盘自动弹起来了,H5input第一次自动弹起IOS和Android中的键盘

场景针对ios和android端需要在H5中首次直接拉起原生键盘,此处需添加原生代码处理android原生中模拟点击事件触发,代码如下publicvoi

场景

针对ios和android端需要在H5中首次直接拉起原生键盘,此处需添加原生代码处理

android

原生中模拟点击事件触发,代码如下

public void showSoftInputMethod(final Callback callback) {

runOnMainThread(new Runnable() {

@Override

public void run() {

WebView webView = mWebViewRef.get();

if (webView != null) {

webView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));

webView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0));

callback.invoke(SUCCESS, "SUCCESS");

} else {

callback.invoke(ERROR, "current webView is null");

}

}});

}

IOS

UIWebView

想一开始唤起键盘,除了web端需要设置input 的focus状态外,我们还需要将keyboardDisplayRequiresUserAction设置为false

WKWebView

WKWebView是没有keyboardDisplayRequiresUserAction这个属性的,但又想做想一开始就能唤起键盘,怎么办呢?只能通过runtime处理了,以下是收集Stack Overflow的方法

#import

@implementationWebViewInjection

+ (void)allowDisplayingKeyboardWithoutUserAction:(BOOL)allow {

Class class = NSClassFromString(@"WKContentView");

NSOperatingSystemVersion iOS_11_3_0 = (NSOperatingSystemVersion){11, 3, 0};

NSOperatingSystemVersion iOS_12_2_0 = (NSOperatingSystemVersion){12, 2, 0};

if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_12_2_0]) {

SEL selector = sel_getUid("_elementDidFocus:userIsInteracting:blurPreviousNode:changingActivityState:userObject:");

Methodmethod =class_getInstanceMethod(class, selector);

if(allowMethod==0x0) {

IMPoriginal =method_getImplementation(method);

allowMethod=imp_implementationWithBlock(^void(idme,void* arg0,BOOLarg1,BOOLarg2,BOOLarg3,idarg4) {

((void(*)(id,SEL,void*,BOOL,BOOL,BOOL,id))original)(me, selector, arg0,TRUE, arg2, arg3, arg4);

});

disAllowMethod= original;

}

if(allow) {

method_setImplementation(method, allowMethod);

}else{

method_setImplementation(method, disAllowMethod);

}

}

else if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_11_3_0]) {

SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:changingActivityState:userObject:");

Methodmethod =class_getInstanceMethod(class, selector);

if(allowMethod==0x0) {

IMPoriginal =method_getImplementation(method);

allowMethod=imp_implementationWithBlock(^void(idme,void* arg0,BOOLarg1,BOOLarg2,BOOLarg3,idarg4) {

((void(*)(id,SEL,void*,BOOL,BOOL,BOOL,id))original)(me, selector, arg0,TRUE, arg2, arg3, arg4);

});

disAllowMethod= original;

}

if(allow) {

method_setImplementation(method, allowMethod);

}

else{

method_setImplementation(method, disAllowMethod);

}

}else{

SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");

Methodmethod =class_getInstanceMethod(class, selector);

if(allowMethod==0x0) {

IMPoriginal =method_getImplementation(method);

allowMethod=imp_implementationWithBlock(^void(idme,void* arg0,BOOLarg1,BOOLarg2,idarg3) {

((void(*)(id,SEL,void*,BOOL,BOOL,id))original)(me, selector, arg0,TRUE, arg2, arg3);

});

disAllowMethod= original;

}

if(allow) {

method_setImplementation(method, allowMethod);

}else{

method_setImplementation(method, disAllowMethod);

}

}

}



推荐阅读
author-avatar
烂哥居士
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有