Zat*_*ter 27 iphone cordova iphone-softkeyboard
我一直在寻找很长很长的时间,到目前为止,我没有遇到过可以通过编程方式显示软键盘的PhoneGap/Cordova应用程序的解决方案.
场景:
我们有一个PhoneGap应用程序 - 一个在jQuery Mobile中创建的网站 - 在某一点上显示了一个用户对话框.此对话框也是一个网页,并且只有一个INPUT文本框,用户应在其中输入代码.
问题:
显示代码对话框时,使用JavaScript聚焦输入框.但是,由于iPhone内部浏览器受到限制,在用户实际真正点击输入文本框之前,软键盘才会出现.
我们尝试了什么:
有任何想法吗?
编辑:此问题中提到的限制仅适用于内置浏览器 ...如果您的目标是Opera,您将通过使用以下代码获得成功:
var e = jQuery.Event("keydown", { keyCode: 37 });
$('#element').focus().trigger(e);
Run Code Online (Sandbox Code Playgroud)
EDIT2:这是一个可以在插件中使用的最终工作PhoneGap代码:
keyboardhelper.h
//
// keyboardHelper.h
// soft keyboard displaying plugin for PhoneGap
//
// Copyright 2012 Martin Ambrus.
//
#import <Foundation/Foundation.h>
#ifdef CORDOVA_FRAMEWORK
#import <Cordova/CDVPlugin.h>
#else
#import "CDVPlugin.h"
#endif
@interface keyboardHelper : CDVPlugin {
NSString *callbackID;
}
@property (nonatomic, copy) NSString *callbackID;
- (void)showKeyboard:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
Run Code Online (Sandbox Code Playgroud)
keyboardhelper.m
//
// keyboardHelper.m
// soft keyboard displaying plugin for PhoneGap
//
// Copyright 2012 Martin Ambrus.
//
#import "keyboardHelper.h"
#import "AppDelegate.h"
@implementation keyboardHelper
@synthesize callbackID;
-(void)showKeyboard:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
self.callbackID = [arguments pop];
//Get text field coordinate from webview. - You should do this after the webview gets loaded
//myCustomDiv is a div in the html that contains the textField.
int textFieldContainerHeightOutput = [[((AppDelegate *)[[UIApplication sharedApplication] delegate]).viewController.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"myCustomDiv\").offsetHeight;"] intValue];
int textFieldContainerWidthOutput = [[((AppDelegate *)[[UIApplication sharedApplication] delegate]).viewController.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"myCustomDiv\").offsetWidth;"] intValue];
int textFieldContainerYOffset = [[((AppDelegate *)[[UIApplication sharedApplication] delegate]).viewController.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"myCustomDiv\").offsetTop;"] intValue];
int textFieldContainerXOffset = [[((AppDelegate *)[[UIApplication sharedApplication] delegate]).viewController.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"myCustomDiv\").offsetLeft;"] intValue];
UITextField *myTextField = [[UITextField alloc] initWithFrame: CGRectMake(textFieldContainerXOffset, textFieldContainerYOffset, textFieldContainerWidthOutput, textFieldContainerHeightOutput)];
[((AppDelegate *)[[UIApplication sharedApplication] delegate]).viewController.webView addSubview:myTextField];
myTextField.delegate = self;
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @"ok"];
[self writeJavascript:[pluginResult toSuccessCallbackString:self.callbackID]];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//here you create your request to the server
return NO;
}
-(BOOL)textFieldDidEndEditing:(UITextField *)textField
{
//here you create your request to the server
return NO;
}
@end
Run Code Online (Sandbox Code Playgroud)
JavaScript的
var keyboardHelper = {
showKeyboard: function(types, success, fail) {
return Cordova.exec(success, fail, "keyboardHelper", "showKeyboard", types);
}
};
Run Code Online (Sandbox Code Playgroud)
小智 12
config.xml这些天您可以通过条目解决问题,添加:
<preference name="keyboardDisplayRequiresUserAction" value="false" />
Run Code Online (Sandbox Code Playgroud)
您可以使用webView上的javascript获取输入字段的坐标.然后,将您自己的textField放在它的顶部,并在其委托方法textFieldShouldReturn中向服务器发送一个请求,其中包含用户输入的代码.
//Get text field coordinate from webview. - You should do this after the webview gets loaded
//myCustomDiv is a div in the html that contains the textField.
int textFieldContainerHeightOutput = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"myCustomDiv\").offsetHeight;"] intValue];
int textFieldContainerWidthOutput = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"myCustomDiv\").offsetWidth;"] intValue];
int textFieldContainerYOffset = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"myCustomDiv\").offsetTop;"] intValue];
int textFieldContainerXOffset = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"myCustomDiv\").offsetLeft;"] intValue];
myTextField.frame = CGRectMake(textFieldContainerXOffset, textFieldContainerYOffset, textFieldContainerWidthOutput, textFieldContainerHeightOutput);
[webView addSubview:myTextField];
myTextField.delegate = self;
Run Code Online (Sandbox Code Playgroud)
然后实现textFieldShouldReturn并在那里向服务器创建请求.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//here you create your request to the server
return NO;
}
Run Code Online (Sandbox Code Playgroud)
这是在现有项目中完成的,但不使用PhoneGap.我希望你能适应它,以满足你的需求.
要删除文本字段,可以隐藏它
myTextField.hidden = YES;
Run Code Online (Sandbox Code Playgroud)
要么
myTextField = nil;
Run Code Online (Sandbox Code Playgroud)
要么
[myTextField removeFromSuperView];
Run Code Online (Sandbox Code Playgroud)
在iOS 6之前,Apple只允许在用户交互后调出键盘.在iOS 6中,他们为UIWebView引入了以下属性,您只需将其设置为NO即可.
"yourWebView".keyboardDisplayRequiresUserAction = NO;
Run Code Online (Sandbox Code Playgroud)
Apple默认将此设置为"是".现在你可以调用focus()你的JS,键盘会出现.
| 归档时间: |
|
| 查看次数: |
20962 次 |
| 最近记录: |