[英]iPhone UIWebview: How to force a numeric keyboard? Is it possible?
我正在嘗試使用PhoneGap開發一些iPhone應用程序。 PhoneGap基本上包裝了一個UIWebView - 它運行良好。 問題是我的應用程序有幾個輸入字段只接受數字輸入。 我真的需要強制數字小鍵盤而不是接受默認的標准鍵盤,然后強制用戶切換到每個字段上的數字。 有誰知道這是否可能? 怎么樣?
澄清:我知道Apple目前不提供任何API來允許此操作。 我想知道創建一個從UIWebView繼承的自定義類是否有幫助,或者創建自定義鍵盤是否會開辟一些可能性?
2009年11月6日更新 - 如下所述,Apple最近更新了safari功能,以便再次支持。 所以接受的答案被改變以反映這一點。
<html>
<input type='tel'/>
<input type='number'/>
<input type='email'/>
<input />
</html>
看起來Mobile Safari支持電子郵件 , 號碼 , 搜索 , 電話和網址的新HTML5輸入類型屬性。 這些將切換顯示的鍵盤。 請參閱type屬性 。
例如,你可以這樣做:
<input type="number" />
當輸入框有焦點時,會顯示數字鍵盤(好像用戶有完整的鍵盤並點擊“123”按鈕。
如果你真的只想要數字,你可以指定:
<input type="tel" />
然后用戶將獲得電話號碼撥號鍵盤。
我知道這適用於Mobile Safari - 我只假設它可以與UIWebView一起使用。
我發現更好的解決方案是在為移動和桌面瀏覽器設計表單時使用<input type="text" pattern="[0-9]*">
。
經過對IPhone OS 3.0,這個功能還是不存在的,不知道為什么蘋果只是刪除這個方便的功能,真的很心煩到現在這一權利工作:(
從Apple的文檔 (關於UIWebView的說明):
您無法在輸入元素中指定鍵盤類型。 Web視圖顯示基於默認鍵盤的自定義鍵盤,但包括一些用於在表單元素之間導航的附加控件。
以下是與iOS(4.0及更高版本)Web視圖兼容的所有類型的列表:
http://conecode.com/news/2011/12/mobile-safari-uiwebview-input-types/
這在iPhone OS的第一次迭代中是可能的 - Safari會給數字鍵盤形成名稱中帶有“zip”或“phone”的字段 - 但它在iPhone OS 2.0中消失了。
PhoneGap目前沒有API函數來覆蓋它。 它可能是他們正在努力的東西,它絕對是一個漂亮的功能。
您需要在Dashcode項目的HTML部分進行更改:
type="text"
更改為type="number"
。 完成。
iOS Mobile Safari還支持:
<input type="password" />
iOs有一個數字鍵盤UIKeyboardTypeNumbersAndPunctuation
,但不能從HTML調用( https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html )
因為在“tel”鍵盤上缺少標點符號,你必須自己創建。 由於ios8自定義鍵盤可用。 或者,如果您只需要標點符號,則可以在數字鍵盤上添加一個按鈕( 如何在iOS中為numpad鍵盤添加“完成”按鈕 ),當您使用type="number" pattern="[0-9]*"
指定輸入字段時彈出的數字鍵盤彈出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);
}
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.