gpt4 book ai didi

c# - Monotouch UITextField 只有数字

转载 作者:太空狗 更新时间:2023-10-30 00:15:28 25 4
gpt4 key购买 nike

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

另外我希望不超过 8 个字符。

最佳答案

为了在 MonoTouch 中仅实现数字 UITextField,我使用以下代码:

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

关于c# - Monotouch UITextField 只有数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14265006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com