【问题标题】:Numbers Keyboard in XcodeXcode 中的数字键盘
【发布时间】:2011-02-07 17:03:41
【问题描述】:

有谁知道如何在 iphone 中显示数字键盘而不是默认键盘?有没有办法只过滤数字的文本字段?我需要通过代码而不是界面生成器来做到这一点

谢谢

【问题讨论】:

  • 从 2013 年开始 ... 只需在 InterfaceBuilder 中随意设置

标签: iphone xcode keyboard textfield


【解决方案1】:

或者,在代码中设置文本字段的keyboardType属性:

textField.keyboardType = UIKeyboardTypeNumberPad

可用选项有:

UIKeyboardType 要输入的键盘类型 显示给定的基于文本的视图。

typedef 枚举 {
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable } UIKeyboardType;

请注意,NumberPad 没有输入小数点。如果你需要,你将不得不使用其他东西。

【讨论】:

    【解决方案2】:

    现在有一个带小数的键盘,你可以使用下面的代码。

    textField.keyboardType = UIKeyboardTypeDecimalPad;

    【讨论】:

      【解决方案3】:

      在 Interface Builder 中或通过代码将输入字段的 keyboardType 属性设置为数字。

      这样的事情应该可以工作:

      textField.keyboardType = UIKeyboardTypeNumberPad;
      

      有关所有可能的键盘类型,请参阅the Apple docs

      【讨论】:

      • @Jeff Kelley 在 thary 发表评论后添加了代码。
      【解决方案4】:

      为了补充 Kristopher Johnson 的答案,UITextField 支持 UITextInputTraits 协议,该协议有一个 UIKeyboardType @property 称为 keyboardType。您只需将其设置为您想要的任何内容(在您的情况下,UIKeyboardTypeNumberPad)。将它放在代码中的一个地方是视图控制器的 viewDidLoad 方法。

      textField.keyboardType = UIKeyboardTypeNumberPad;
      

      【讨论】:

        【解决方案5】:

        对于 Swift2

        keyboardType 正在等待 UIKeyboardType

        枚举 UIKeyboardType : Int {
        案例 默认
        case ASCIICapable
        案例 NumbersAndPunctuation
        案例网址
        案例 NumberPad
        手机壳
        案例名称PhonePad
        案例电子邮件地址
        案例 DecimalPad
        案例推特
        案例网络搜索
        static var Alphabet: UIKeyboardType { 获取 }
        }

        所以应该是这样的:

        textField.keyboardType = UIKeyboardType.NumbersAndPunctuation
        

        Apple Documentation - UIKeyboardType

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-09
          • 2011-07-23
          • 1970-01-01
          • 2014-04-13
          • 2017-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多