代码之家  ›  专栏  ›  技术社区  ›  Wasif Mahmood Mustafa

在xamarin格式中,只将条目限制为数字和句号

  •  0
  • Wasif Mahmood Mustafa  · 技术社区  · 6 年前

    我有一个条目,我只限于数字(它不能接受任何其他符号或字母),现在我希望它只接受句号(.)

    这是只允许输入数字的代码。

    private static void OnEntryTextChanged(object sender, TextChangedEventArgs args)
        {
    
            if (!string.IsNullOrWhiteSpace(args.NewTextValue))
            {
                bool isValid = args.NewTextValue.ToCharArray().All(x => char.IsDigit(x)); //Make sure all characters are numbers
    
                ((Entry)sender).Text = isValid ? args.NewTextValue : args.NewTextValue.Remove(args.NewTextValue.Length - 1);
            }
        }
    

    有人能帮忙吗?谢谢。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jason    6 年前
    bool isValid = args.NewTextValue.ToCharArray().All(x => char.IsDigit(x) || x.Equals('.'));