iPhone UIWeb 视图:如何强制使用数字键盘?是否可以?
题
我正在尝试使用 PhoneGap 来开发一些 iPhone 应用程序。PhoneGap 基本上包装了一个 UIWebView - 它运行良好。问题是我的应用程序有几个仅接受数字输入的输入字段。我确实需要强制使用数字键盘,而不是接受默认的标准键盘,然后强制用户在每个字段上切换到数字。有谁知道这是否可能?如何?
澄清:我知道苹果目前没有提供任何 API 来允许这样做。我想知道创建一个从 UIWebView 继承的自定义类是否会有帮助,或者创建一个自定义键盘是否会带来一些可能性?
2009 年 11 月 6 日更新 - 如下所述,Apple 最近更新了 safari 功能,以便再次支持该功能。因此,已接受的答案已更改以反映这一点。
<html>
<input type='tel'/>
<input type='number'/>
<input type='email'/>
<input />
</html>
解决方案
它看起来像移动Safari支持电子邮件,号,搜索,电话和 URL 的。这将切换显示的键盘。请参阅 type属性
因此,例如,你可以这样做:
<input type="number" />
和当输入框具有焦点,数字键盘被示出(如,如果用户具有全键盘和打“123”按钮。
如果你真的只想要的数字,你可以指定:
<input type="tel" />
,然后用户将获得的电话号码拨号键盘。
我知道这工作与移动Safari浏览器 - 我只认为它会与UIWebView的工作
其他提示
我发现了一个更好的解决方案是设计形成用于移动设备和桌面浏览器时使用<input type="text" pattern="[0-9]*">
。
从 苹果的文档 (关于UIWebView的注释):
您无法在输入元素中指定键盘类型。Web 视图显示一个基于默认键盘的自定义键盘,但包含一些用于在表单元素之间导航的附加控件。
,这个功能还是不存在的,不知道为什么苹果只是删除这个方便的功能,真的很心烦对现在这个工作的权利:(
下面是每个类型与iOS(4.0或更高)的网络视图兼容的列表:
这在iPhone OS的第一次迭代是可能的 - Safari浏览器将使数字键盘形成在其名称中“压缩”或“手机”领域 - 但它在iPhone OS 2.0
消失。的PhoneGap不具有API函数的时刻,以覆盖此。这可能是他们正在努力的东西,它肯定会是一个漂亮的功能。
iOS移动的Safari还支持:
<input type="password" />
您需要对 Dashcode 项目的 HTML 部分进行更改:
- 在 Dashcode 中,在代码窗口中打开 index.html。
- 在您正在处理的视图的画布上,选择要设置为使用优化键盘的输入字段。
- 单击index.html 使其获得焦点。
- 从 Dashcode 菜单栏中选择编辑 > 查找。
- 在查找框中键入您为文本字段控件指定的确切名称。Find 将在index.html 文件中定位该控件。
- 更改类型从
type="text"
到type="number"
.
完毕。
iOs 有数字键盘 UIKeyboardTypeNumbersAndPunctuation
, ,但这不能从 HTML 调用(https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html)
由于“tel”键盘上缺少标点符号,因此您必须自己创建它。自ios8自定义键盘可用以来。或者,如果您只需要标点符号,您可以添加一个按钮(如何在 iOS 中的小键盘上添加“完成”按钮) 到当您指定输入字段时弹出的数字键盘 type="number" pattern="[0-9]*"
.
为此:Objective-C 代码。
-(void)keyboardWillHide:(NSNotification *)note
{
[self.donekeyBoardBtn removeFromSuperview];
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat: @"document.activeElement.blur()"]];
}
- (void)keyboardWillShow:(NSNotification *)note
{
[UIView setAnimationsEnabled:NO];
// create custom button
//UIKeyboardTypeNumberPadin
//type="number" pattern="[0-9]*"
UIButton *extryB= [UIButton buttonWithType:UIButtonTypeRoundedRect];
extryB.frame = CGRectMake(0, self.view.frame.size.height+150, 100, 50);
extryB.backgroundColor = [UIColor colorWithRed:204.0f/255.0f
green:210.0f/255.0f
blue:217.0f/255.0f
alpha:1.0f];
extryB.adjustsImageWhenHighlighted = NO;
extryB.titleLabel.font = [UIFont systemFontOfSize:14.0];
[extryB setTitle:@"," forState:UIControlStateNormal];
[extryB setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[extryB addTarget:self action:@selector(extryBpressed) forControlEvents:UIControlEventTouchUpInside];
self.donekeyBoardBtn = extryB;
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)
{
for(int i = 0 ; i < [keyboard.subviews count] ; i++)
{
UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES)
{
UIButton* donebtn = (UIButton*)[hostkeyboard viewWithTag:67123];
if (donebtn == nil){
//to avoid adding again and again as per my requirement (previous and next button on keyboard)
if([[self printKeyboardType] isEqualToString: @"UIKeyboardTypePhonePad"]){
[keyboard addSubview:extryB];
}
}
}
}
}
//[keyboard addSubview:extryB];
}
[UIView setAnimationsEnabled:YES];
// animate
[UIView animateWithDuration:0.5 animations:^{
extryB.frame = CGRectMake(0, self.view.frame.size.height-50, 100, 50);
}];
}
-(NSString*)printKeyboardType
{
NSString* strKeyboardType = @"";
switch (self.textDocumentProxy.keyboardType) {
case UIKeyboardTypeAlphabet:
strKeyboardType = @"UIKeyboardTypeAlphabet";
break;
case UIKeyboardTypeDecimalPad:
strKeyboardType = @"UIKeyboardTypeAlphabet";
break;
case UIKeyboardTypeDefault:
strKeyboardType = @"UIKeyboardTypeDefault";
break;
case UIKeyboardTypeEmailAddress:
strKeyboardType = @"UIKeyboardTypeEmailAddress";
break;
case UIKeyboardTypeTwitter:
strKeyboardType = @"UIKeyboardTypeTwitter";
break;
case UIKeyboardTypeNamePhonePad:
strKeyboardType = @"UIKeyboardTypeNamePhonePad";
break;
case UIKeyboardTypeNumberPad:
strKeyboardType = @"UIKeyboardTypeNumberPad";
break;
case UIKeyboardTypeNumbersAndPunctuation:
strKeyboardType = @"UIKeyboardTypeNumbersAndPunctuation";
break;
case UIKeyboardTypePhonePad:
strKeyboardType = @"UIKeyboardTypePhonePad";
break;
case UIKeyboardTypeURL:
strKeyboardType = @"UIKeyboardTypeURL";
break;
case UIKeyboardTypeWebSearch:
strKeyboardType = @"UIKeyboardTypeWebSearch";
break;
default:
strKeyboardType = @"UNKNOWN";
break;
}
NSLog(@"self.textDocumentProxy.keyboardType=%@", strKeyboardType);
return strKeyboardType;
}
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
- (void)extryBpressed{
//simulates a "." press
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat: @"simulateKeyPress('.');"]];
}
然后您应该在 JS 代码中添加一个可以在应用程序范围内访问的方法。
simulateKeyPress: function(k) {
var press = jQuery.Event("keypress");
press.which = k;
// place the id of your most outer html tag here
$("#body").trigger(press);
// just needed because my active element has a child element where the value should be placed in
var id = document.activeElement.id.indexOf("-");
if(id != -1){
var currentTf = sap.ui.getCore().byId(document.activeElement.id.split("-")[0]);
//append the value and set it (my textfield has a method setValue())
var currentTfValue = currentTf.getValue();
currentTf.setValue(currentTfValue + k);
}
},