如何限制用户输入EditText(Xamarin.Android)中小数点分隔符后的最大2位或3位数()

卧疾丰暇豫,翰墨时间作。这篇文章主要讲述如何限制用户输入EditText(Xamarin.Android)中小数点分隔符后的最大2位或3位数?相关的知识,希望能为你提供帮助。
我有以下课程:

public class DecimalFilter : java.Lang.Object, IInputFilter { //Pattern mPattern; String regex = "[0-9]+((\.[0-9]{0," + (2 - 1) + "})?)||(\.)?"; public DecimalFilter(int digitsAfterZero) { //mPattern = Pattern.compile("[0-9]+((\.[0-9]{0," + (digitsAfterZero - 1) + "})?)||(\.)?"); regex = "[0-9]+((\.[0-9]{0," + (digitsAfterZero - 1) + "})?)||(\.)?"; }public Java.Lang.ICharSequence FilterFormatted(Java.Lang.ICharSequence source, int start, int end, ISpanned dest, int dstart, int dend) { if (System.Text.RegularExpressions.Regex.IsMatch(dest.ToString(), regex)) { return new Java.Lang.String(string.Empty); } return null; } }

我有两个EditText,我在其上设置过滤器:
discountEditText.SetFilters(new IInputFilter[] { new DecimalFilter(2) }); quantityEditText.SetFilters(new IInputFilter[] { new DecimalFilter(3) });

当我运行应用程序并尝试在这些EditTexts中输入内容时,我甚至无法输入单个数字。
答案【如何限制用户输入EditText(Xamarin.Android)中小数点分隔符后的最大2位或3位数()】试试这段代码:
public class DecimalFilter : Java.Lang.Object, IInputFilter { String regex = "[0-9]+((\.[0-9]{0," + (2 - 1) + "})?)||(\.)?"; public DecimalFilter(int digitsAfterZero) { //mPattern = Pattern.compile("[0-9]+((\.[0-9]{0," + (digitsAfterZero - 1) + "})?)||(\.)?"); regex = "^[0-9]+(.[0-9]{0," + (digitsAfterZero-1)+ "})?$"; }public Java.Lang.ICharSequence FilterFormatted(Java.Lang.ICharSequence source, int start, int end, ISpanned dest, int dstart, int dend) {if (System.Text.RegularExpressions.Regex.IsMatch(dest.ToString(), regex)||dest.ToString().Equals("")) { return new Java.Lang.String(source.ToString()); }return new Java.Lang.String( string.Empty); } }


    推荐阅读