【问题标题】:Monotouch UITextField only numbersMonotouch UITextField 只有数字
【发布时间】:2013-01-10 18:41:53
【问题描述】:

我没有找到任何解决方案如何限制 textfield 在 Monotouch 中只允许数字。

我也希望不超过 8 个字符。

【问题讨论】:

    标签: c# xamarin.ios uitextfield character


    【解决方案1】:

    要在 MonoTouch 中仅实现数字 UITextFields,我使用以下代码:

    var numberTextField = new UITextField { KeyboardType = UIKeyboardType.NumberPad };
    numberTextField.ShouldChangeCharacters = (textField, range, replacement) =>
        {
            int number;
            return replacement.Length == 0 || int.TryParse(replacement, out number);
        };
    

    并限制特定数量的字符:

    const int maxCharacters = 8;
    textField.ShouldChangeCharacters = (textField, range, replacement) =>
        {
            var newContent = new NSString(textField.Text).Replace(range, new NSString(replacement)).ToString();
            return newContent.Length <= maxCharacters;
        };
    

    所以,结合起来:

    const int maxCharacters = 8;
    var numberTextField = new UITextField { KeyboardType = UIKeyboardType.NumberPad };
    numberTextField.ShouldChangeCharacters = (textField, range, replacement) =>
        {
            var newContent = new NSString(textField.Text).Replace(range, new NSString(replacement)).ToString();
            int number;
            return newContent.Length <= maxCharacters && (replacement.Length == 0 || int.TryParse(replacement, out number));
        };
    

    【讨论】:

      【解决方案2】:

      实现这一点的最简单方法是创建您自己的从UITextField 继承的控件,然后创建一个从UITextFieldDelegate 继承的委托并将其分配为控件的Delegate

      在代理的ShouldChangeCharacters覆盖方法中,你可以对新输入的文本进行适当的测试,如果它无效,你可以返回false来阻止文本字段被更新。

      【讨论】:

      • 好的,有没有办法用 ShouldChangeTextInRange 方法做到这一点?
      • 是的,对于 UITextView,您将执行与上述相同的顺序,但使用委托的 ShouldChangeText 方法。
      【解决方案3】:

      在 MonoTouch/C# 中,这是将文本字段限制为数字的更好方法。

      textCardNumber.ShouldChangeCharacters = (UITextField textField, NSRange range, string replace) => {
              int dummy;
              return replace.Length == 0 || int.TryParse (replace, out dummy);
      };
      

      编辑: 添加了能够删除的功能。

      【讨论】:

      • 由于replace.Length &gt; 0,您的答案不允许从文本字段中删除字符。
      • 不应该是 ||而不是 &&?
      【解决方案4】:

      如果你想子类化 UITextField 以获得可重用的代码,这就是我所做的。

      [Register("CustomTextField")]
      public class CustomTextField : UITextField
      {
      public CustomTextField() : base() {}
      
      public CustomTextField(IntPtr handle) : base(handle)
      {
           Delegate = new CustomDelegate();
      }
      
      private class CustomDelegate : UITextFieldDelegate
      {
           public override bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
           {
                int maxCharacters = 8;
                var newContent = new NSString(textField.Text).Replace(range, new NSString(replacement)).ToString();
                int number;
                return newContent.Length <= maxCharacters && (replacement.Length == 0 || int.TryParse(replacement, out number));
           }
      }
      

      ShouldChangeCharacters 的内容只是从 redent84 的答案中复制和粘贴的。我只想给出这个答案来展示如何继承 UITextField。

      另外,如果你使用我的方法,你不能像 Redent84 那样在 UITextField 上使用委托,因为你会得到一个运行时错误,说委托已经分配。

      如果不创建委托子类,我不知道该怎么做。

      另外,不要忘记将可视化编辑器中文本字段的类更改为 CustomTextField 或您决定命名的任何名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-11
        • 1970-01-01
        • 2011-11-24
        • 2012-10-08
        • 2014-06-29
        • 2012-05-30
        • 1970-01-01
        相关资源
        最近更新 更多