【发布时间】:2012-08-27 10:44:33
【问题描述】:
我一直在寻找很长一段时间,但到目前为止,我还没有找到一个可行的解决方案,用于以编程方式显示软键盘的 PhoneGap / Cordova 应用程序。
场景:
我们有一个PhoneGap 应用程序——一个用jQuery Mobile 创建的网站——它在某一时刻向用户显示一个对话框。此对话框也是一个网页,只有一个 INPUT 文本框,用户应在其中输入代码。
问题:
显示代码对话框时,输入框使用 JavaScript 聚焦。但是,由于 iPhone 内部浏览器的限制,直到用户真正在输入文本框内单击时才会出现软键盘。
我们尝试了什么:
- 创建 hidden text box 并使其成为第一响应者
- 一旦输入通过 JavaScript 获得焦点,就让实际的 webview 成为第一响应者
- 使用 sendActionsForControlEvents 尝试将触摸事件传递到 web 视图(尽管如果有人有 PhoneGap 应用程序的工作代码,如果他们能分享它,我将不胜感激,因为我不是 iOS 编码方面的专业人士)
有什么想法吗?
编辑:此问题中提到的限制仅适用于 内置浏览器...如果您的目标是 Opera,您使用以下代码将成功:
var e = jQuery.Event("keydown", { keyCode: 37 });
$('#element').focus().trigger(e);
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
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
javascript
var keyboardHelper = {
showKeyboard: function(types, success, fail) {
return Cordova.exec(success, fail, "keyboardHelper", "showKeyboard", types);
}
};
【问题讨论】:
-
你试过$("#input_id").trigger("click")吗?我还没有,因为我在工作,但这是您可以尝试的更多选择。
-
是的,我们尝试过...这里不是关于 JavaScript,因为由于可用性和性能问题,编程事件永远不会触发移动设备上的软键盘...我正在寻找原生 iPhone可以在这里提供帮助的代码
-
我已经更新了问题以包含一个似乎不受键盘限制影响的 Opera Mobile 浏览器的解决方案
-
downvoter - 需要解释吗?
标签: iphone cordova iphone-softkeyboard