gpt4 book ai didi

iphone - 在 PhoneGap 应用程序中以编程方式在 iPhone 上显示软键盘?

转载 作者:行者123 更新时间:2023-12-03 18:16:44 24 4
gpt4 key购买 nike

我已经进行了很长时间的搜索,到目前为止,我还没有找到能够以编程方式显示软键盘的 PhoneGap/Cordova 应用程序的有效解决方案

场景:

我们有一个 PhoneGap 应用程序 - 一个在 jQuery Mobile 中创建的网站 - 在某一时刻向用户显示一个对话框。该对话框也是一个网页,并且有一个输入文本框,用户应在其中输入代码。

问题:

显示代码对话框时,使用 JavaScript 聚焦输入框。但是,由于 iPhone 内部浏览器的限制,只有当用户真正在输入文本框内单击时,软键盘才会出现。

我们尝试过的:

  • 创建一个 hidden text box 并使其成为第一响应者
  • 一旦输入通过 JavaScript 获得焦点,就使实际的webview成为第一响应者
  • 使用sendActionsForControlEvents尝试将触摸事件传递到 WebView (尽管如果有人有PhoneGap应用程序的工作代码,如果他们可以分享它,我将不胜感激,因为我不是 iOS 编码方面的专业人士)

有什么想法吗?

<小时/>

编辑:这个问题中提到的限制仅适用于内置浏览器...如果您的目标是 Opera,那么您使用以下代码即可成功:

var e = jQuery.Event("keydown", { keyCode: 37 });
$('#element').focus().trigger(e);
<小时/>

编辑2:这是可在插件中使用的最终工作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);
}
};

最佳答案

现在您可以通过 config.xml 条目解决问题,添加:

<preference name="keyboardDisplayRequiresUserAction" value="false" />

关于iphone - 在 PhoneGap 应用程序中以编程方式在 iPhone 上显示软键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12140484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com