在PhoneGap应用程序中以编程方式在iPhone上显示软键盘?
我已经搜索了很长时间,到目前为止,我还没有找到一个能以编程方式显示软键盘的PhoneGap/Cordova应用程序的工作解决方案 场景: 我们有一个PhoneGap应用程序——一个在jQuery Mobile中创建的网站——它在某一点上向用户显示了一个对话框。此对话框也是一个网页,只有一个输入文本框,用户应在其中输入代码 问题: 显示代码对话框时,将使用JavaScript聚焦输入框。然而,由于iPhone内部浏览器的限制,在用户真正点击输入文本框之前,软键盘不会出现 我们尝试的内容:在PhoneGap应用程序中以编程方式在iPhone上显示软键盘?,iphone,cordova,iphone-softkeyboard,Iphone,Cordova,Iphone Softkeyboard,我已经搜索了很长时间,到目前为止,我还没有找到一个能以编程方式显示软键盘的PhoneGap/Cordova应用程序的工作解决方案 场景: 我们有一个PhoneGap应用程序——一个在jQuery Mobile中创建的网站——它在某一点上向用户显示了一个对话框。此对话框也是一个网页,只有一个输入文本框,用户应在其中输入代码 问题: 显示代码对话框时,将使用JavaScript聚焦输入框。然而,由于iPhone内部浏览器的限制,在用户真正点击输入文本框之前,软键盘不会出现 我们尝试的内容: 创建一
- 创建一个并使其成为第一响应者
- 一旦输入通过JavaScript接收焦点,将实际的webview设置为第一响应者
- 使用sendActionsForControlEvents尝试将触摸事件发送到webview(尽管如果任何人有PhoneGap应用程序的工作代码,我希望他们能分享,因为我不擅长iOS编码)
编辑:此问题中提到的限制仅适用于内置浏览器。。。如果您的目标是Opera,您将通过使用以下代码获得成功:
var e = jQuery.Event("keydown", { keyCode: 37 });
$('#element').focus().trigger(e);
EDIT2:这是最后一个可用的PhoneGap代码,可以在插件中使用: 键盘助手 javascript
您是否尝试过使用本机控件并从Javascript调用它们 这里有一些代码显示Phonegap Cordova应用程序(Phonegap 1.5)上本机控件的用法
希望这有助于解决问题:)我承认这是私人的,但它可能会帮助您:
@class UIKeyboard;
void showKeyboard()
{
static UIKeyboard *kb = nil;
if ([[UIKeyboard class] respondsToSelector:@selector(automaticKeyboard)])
kb = [UIKeyboard automaticKeyboard];
else
kb = [UIKeyboard activeKeyboard];
if (kb == nil) {
kb = [[[UIKeyboard alloc] initWithDefaultSize] autorelease];
[[[UIApplication sharedApplication] keyWindow] addSubview:kb];
}
if ([kb respondsToSelector:@selector(orderInWithAnimation:)]) {
[kb orderInWithAnimation:YES];
} else {
[kb activate];
[kb minimize];
[kb maximize];
}
}
这样称呼它:
showKeyboard();
您可以在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;
然后实现textFieldShouldReturn并创建对服务器的请求
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//here you create your request to the server
return NO;
}
这是在现有项目中完成的,但是没有使用PhoneGap。我希望你能调整它以适应你的需要
要删除文本字段,可以将其隐藏
myTextField.hidden = YES;
或
或
在iOS 6之前,苹果只允许在用户交互后启动键盘。在iOS 6中,他们为UIWebView引入了以下属性,您只需将其设置为“否”
"yourWebView".keyboardDisplayRequiresUserAction = NO;
苹果默认设置为“是”。现在您可以在JS中调用
focus()
,键盘就会出现 事实上,我刚刚找到了解决这个问题的方法。
就像horak所说的那样,无论是否使用软键盘,现在都可以通过使用iOS 6.0实现这一点:KeyboardDisplayRequireservation=NO强>
从Cordova 2.2开始,上述iOS属性可以简单地添加到Cordova.plist文件中:
KeyboardDisplayRequiresUserAction, boolean, NO
这为使用Cordova 2.2的所有人解决了所有问题。现在只需调用前面讨论的input.focus(),键盘就会自动出现。对于那些尚未将Cordova更新到当前最新版本(2.2)的人来说,幸运的是,仍然有可能
使用cordova v以编程方式在iPhone上显示键盘2.2
步骤1:
将属性添加到Cordova.plist
转到您的项目>资源>Cordova.plist。Add:KeyboardDisplayRequiresSerAction,布尔值,否
步骤2:
将下面的代码段添加到CDVViewController.m
搜索“@interface CDVViewController”(或类似于查找上述文件)。对于Cordova 2.1,请转到第240行(其他所有人都转到“IsAtLeastiOSVersion”if语句后面的一行,抱歉,再精确不过了。)将此代码段添加到CDVViewController.m的上述行中:
if (IsAtLeastiOSVersion(@"6.0")) {
BOOL keyboardDisplayRequiresUserAction = YES; // KeyboardDisplayRequiresUserAction - defaults to YES
if ([self.settings objectForKey:@"KeyboardDisplayRequiresUserAction"] != nil) {
if ([self.settings objectForKey:@"KeyboardDisplayRequiresUserAction"]) {
keyboardDisplayRequiresUserAction = [(NSNumber*)[self.settings objectForKey:@"KeyboardDisplayRequiresUserAction"] boolValue]; //JB-VAL121212
}
}
// property check for compiling under iOS < 6
if ([self.webView respondsToSelector:@selector(setKeyboardDisplayRequiresUserAction:)]) {
[self.webView setValue:[NSNumber numberWithBool:keyboardDisplayRequiresUserAction] forKey:@"keyboardDisplayRequiresUserAction"];
}
}
if(IsAtLeastiOSVersion(@“6.0”)){
BOOL keyboardDisplayRequiresAction=YES;//keyboardDisplayRequiresAction-默认为YES
if([self.settings objectForKey:@“KeyboardDisplayRequireservation”]!=nil){
如果([self.settings objectForKey:@“KeyboardDisplayRequireservation”]){
KeyboardDisplayRequiresAction=[(NSNumber*)[self.settings objectForKey:@“KeyboardDisplayRequiresAction”]布尔值];//JB-VAL121212
}
}
//iOS<6下编译的属性检查
if([self.webView respondsToSelector:@selector(SetKeyboardDisplayRequireservation:)])){
[self.webView setValue:[NSNumber numberWithBool:KeyboardDisplayRequiresAction]forKey:@“KeyboardDisplayRequiresAction”];
}
}
免责声明:这仅在Cordova 2.1上进行了测试,但是,它很可能仍然适用于2.0或CDVViewController.m文件附带的任何其他早期版本)您可以在输入字段上使用FocusOut事件,当按下Done键时将触发该事件。我可以在IOS 6和更高版本上使用它。我相信它在以前的版本上也可以使用。现在,您可以使用
config.xml
条目来解决这个问题,添加:
<preference name="keyboardDisplayRequiresUserAction" value="false" />
您是否尝试过$(“#输入#id”)。触发器(“单击”)?我还没有,因为我在工作,但这是一个选项,你可以尝试更多。是的,我们尝试了。。。这不是关于JavaScript的问题,因为由于可用性和性能问题,编程事件不会触发移动设备上的软键盘。。。我正在寻找可以帮助解决这个问题的原生iPhone代码我已经更新了这个问题,加入了一个Opera手机浏览器的解决方案,它似乎不受键盘r的影响
[myTextField removeFromSuperView];
"yourWebView".keyboardDisplayRequiresUserAction = NO;
KeyboardDisplayRequiresUserAction, boolean, NO
if (IsAtLeastiOSVersion(@"6.0")) {
BOOL keyboardDisplayRequiresUserAction = YES; // KeyboardDisplayRequiresUserAction - defaults to YES
if ([self.settings objectForKey:@"KeyboardDisplayRequiresUserAction"] != nil) {
if ([self.settings objectForKey:@"KeyboardDisplayRequiresUserAction"]) {
keyboardDisplayRequiresUserAction = [(NSNumber*)[self.settings objectForKey:@"KeyboardDisplayRequiresUserAction"] boolValue]; //JB-VAL121212
}
}
// property check for compiling under iOS < 6
if ([self.webView respondsToSelector:@selector(setKeyboardDisplayRequiresUserAction:)]) {
[self.webView setValue:[NSNumber numberWithBool:keyboardDisplayRequiresUserAction] forKey:@"keyboardDisplayRequiresUserAction"];
}
}
<preference name="keyboardDisplayRequiresUserAction" value="false" />