Xcode 中的数字键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2634081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Numbers Keyboard in Xcode
提问by thary
Does any one know how to show the numbers keyboard in the iphone rather than the default one? And is there a way to filter the textfield for numbers only? I need to do that from the code not from interface builder
有谁知道如何在iphone中显示数字键盘而不是默认键盘?有没有办法只过滤数字的文本字段?我需要从代码而不是界面构建器中做到这一点
Thanks
谢谢
回答by John Wang
alternatively, set the textfield's keyboardType property in code:
或者,在代码中设置文本字段的 keyboardType 属性:
textField.keyboardType = UIKeyboardTypeNumberPad
Available options are:
可用选项有:
UIKeyboardType The type of keyboard to display for a given text-based view.
typedef enum {
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable } UIKeyboardType;
UIKeyboardType 为给定的基于文本的视图显示的键盘类型。
typedef enum {
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable } UIKeyboardType;
A word of caution though, The NumberPad doesn't have a decimal point for entry. If you need that you'll have to use something else.
不过需要注意的是,数字键盘没有输入小数点。如果你需要,你将不得不使用其他东西。
回答by gware
There's a keyboard with a decimal included now, you can use the following code.
现在有一个带小数的键盘,您可以使用以下代码。
textField.keyboardType = UIKeyboardTypeDecimalPad;
textField.keyboardType = UIKeyboardTypeDecimalPad;
回答by Kristopher Johnson
Set the keyboardType
property of the entry field to be numeric, either in Interface Builder or via code.
keyboardType
在 Interface Builder 中或通过代码将输入字段的属性设置为数字。
Something like this should work:
这样的事情应该工作:
textField.keyboardType = UIKeyboardTypeNumberPad;
For all the possible keyboard types see the Apple docs
对于所有可能的键盘类型,请参阅Apple 文档
回答by Shaggy Frog
To add to Kristopher Johnson's answer, UITextField
supports the UITextInputTraits
protocol, which has a UIKeyboardType
@property
called keyboardType
. You just set that to be whatever you want (in your case, UIKeyboardTypeNumberPad
). One place to put this in your code is in the viewDidLoad
method of your view controller.
添加到 Kristopher Johnson 的答案中,UITextField
支持UITextInputTraits
具有UIKeyboardType
@property
称为keyboardType
. 您只需将其设置为您想要的任何内容(在您的情况下,UIKeyboardTypeNumberPad
)。把它放在你的代码中的一个地方是viewDidLoad
你的视图控制器的方法。
textField.keyboardType = UIKeyboardTypeNumberPad;
回答by Zat42
For Swift2
对于 Swift2
keyboardType is waiting a UIKeyboardType
keyboardType 正在等待UIKeyboardType
enum UIKeyboardType : Int {
case Default
case ASCIICapable
case NumbersAndPunctuation
case URL
case NumberPad
case PhonePad
case NamePhonePad
case EmailAddress
case DecimalPad
case Twitter
case WebSearch
static var Alphabet: UIKeyboardType { get }
}
enum UIKeyboardType : Int {
case Default
case ASCIICapable
case NumbersAndPunctuation
case URL
case NumberPad
case PhonePad
case NamePhonePad
case EmailAddress
case DecimalPad
case Twitter
case WebSearch
static var Alphabet: UIKeyboardType { get }
}
So it should be something like this :
所以它应该是这样的:
textField.keyboardType = UIKeyboardType.NumbersAndPunctuation