UITextField – iPad上的UIKeyboardTypeDecimalPad?

在iPad上…

textField.keyboardType = UIKeyboardTypeDecimalPad; 

…只是显示普通的键盘,但从顶部的数字开始(下面有很多标点符号)。

但是要input一个数字。 我只希望用户能够input数字和小数点,就像在iPhone上的UIKeyboardTypeDecimalPad一样。

有没有办法让不相关的钥匙离开,离开我的用户?

设置UITextFieldkeyboardType只能让用户input合适的字符。 即使在iPhone上,用户也可以通过硬件键盘input其他字符,或者粘贴string。

相反,实现UITextFieldDelegate-textField:shouldChangeCharactersInRange:replacementString:validation用户input。 你也可能不是真的想硬编码数字0-9和一个句点。 例如,某些语言环境中的用户使用逗号将整数与小数分开:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *candidate = [[textField text] stringByReplacingCharactersInRange:range withString:string]; if (!candidate || [candidate length] < 1 || [candidate isEqualToString:@""]) { return YES; } NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:candidate]; if (!number || [number isEqualToNumber:[NSDecimalNumber notANumber]]) { return NO; } return YES; } 

或者,您可以在用户完成input文本时执行validation,可根据需要在–textFieldShouldReturn: – textFieldShouldEndEditing:–textFieldShouldReturn:进行validation。

这不一定是一个万无一失的解决scheme,但它符合我的需求,可能有助于他人。 关心的是数字字符是硬编码的。 我不确定使用其他数字字符的区域设置,但可以根据需要添加它们。

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *candidate = [[textField text] stringByReplacingCharactersInRange:range withString:string]; // Ensure that the local decimal seperator is used max 1 time NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease]; NSString *decimalSymbol = [formatter decimalSeparator]; if ([candidate componentsSeparatedByString:decimalSymbol].count > 2) return NO; // Ensure that all the characters used are number characters or decimal seperator NSString *validChars = [NSString stringWithFormat:@"0123456789%@", decimalSymbol]; if ([candidate stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:validChars]].length) return NO; return YES; } 

我知道这个问题已经停止了一段时间,但这可能会帮助那里的人。

你可以尝试执行这个https://github.com/azu/NumericKeypad 。 编码器实现了他自己的数字键盘购买子类的UITextField

将其链接到您的textField的editingChanged方法。 您可以根据需要编辑周围的字符。

 - (IBAction)numberValidator:(id)sender { NSMutableString *txt1 = [[NSMutableString alloc] initWithString:_numbersOnlyField.text]; for (unsigned int i = 0; i < [txt1 length]; i++) { NSString *character = [[NSString alloc] initWithFormat:@"%C", [txt1 characterAtIndex:i]]; // Erase any characters you don't like, but you don't feel the user needs to be alerted about else if ([character isEqualToString:@" "]){ [txt1 deleteCharactersInRange:NSMakeRange(i, 1)]; _numbersOnlyField.text = [[NSString alloc] initWithString:txt1]; } // Alert the user that they shouldn't enter anything besides numbers if ([character integerValue] == 0 && ![character isEqualToString:@"0"]) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Numbers only please!" message: @"Please only enter numbers in the \"<your text field's name here>\" text field" delegate: nil cancelButtonTitle:@"Sorry..." otherButtonTitles:nil]; [alert show]; [txt1 deleteCharactersInRange:NSMakeRange(i, 1)]; _numbersOnlyField.text = [[NSString alloc] initWithString:txt1]; } } } 

你也可以通过UIControlEventValueChanged方法编程。

这只是我自己提出的一个解决scheme,我绝对不认为自己是一个专业人士,所以可能有一些我可以忽略的缺陷,但是这对我来说很好。