简体   繁体   中英

Monotouch UITextField only numbers

I don't find any solution how to limit a textfield do allow only numbers in Monotouch.

Also I want that there a not more than 8 characters.

To implement numeric only UITextField s in MonoTouch I use this code:

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

And to limit to a specific amount of characters:

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;
    };

So, combined:

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));
    };

The easiest way to implement this is to create your own control that inherits from UITextField , then create a delegate that inherits from UITextFieldDelegate and assign this as the control's Delegate .

In the delegate's ShouldChangeCharacters override method, you can perform the appropriate tests on the newly entered text and, if it is not valid, you can return false to prevent the text field from being updated.

In MonoTouch/C#, here is a better way to restrict a text field to numbers.

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

EDIT: Added ability to be able to delete.

If you want to subclass UITextField for reusable code this is what I did.

[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));
     }
}

The contents of ShouldChangeCharacters was just copied and pasted from redent84's answer. I just want to give this answer to show how to subclass UITextField.

Also, if you use my method, you can't use the delegate on UITextField like Redent84's because you'll get a runtime error saying the delegate is already assigned.

I don't know how to do this without creating a delegate subclass.

Also, don't forget to change the class of the text field in the visual editor to be CustomTextField or whatever you decide to name it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM