我一直在寻找很长时间,到目前为止,我还没有遇到一个可以以编程方式显示软键盘的 PhoneGap / Cordova 应用程序的工作解决方案。
设想:
我们有一个PhoneGap 应用程序——一个用jQuery Mobile 创建的网站——它在某一时刻向用户显示一个对话框。此对话框也是一个网页,并且有一个单一的 INPUT 文本框,用户应在其中输入代码。
问题:
当显示代码对话框时,输入框使用 JavaScript 聚焦。然而,由于 iPhone 内部浏览器的限制,软键盘直到用户真正点击输入文本框后才会出现。
我们尝试了什么:
- 创建一个隐藏的文本框并使其成为第一响应者
- 一旦输入通过 JavaScript 获得焦点,使实际的webview 成为第一响应者
- 使用sendActionsForControlEvents尝试将 Touch 事件传递给 webview(尽管如果有人有 PhoneGap 应用程序的工作代码,如果他们能分享它,我将不胜感激,因为我不是 iOS 编码方面的专业人士)
有任何想法吗?
编辑:这个问题中提到的限制仅适用于内置浏览器......如果您的目标是 Opera,您将通过使用以下代码获得成功:
var e = jQuery.Event("keydown", { keyCode: 37 });
$('#element').focus().trigger(e);
EDIT2:这是可以在插件中使用的最终工作PhoneGap代码:
键盘助手.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
键盘助手.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);
}
};