示例#1
0
        public static void UpdateMaxLength(this AppCompatEditText editText, int maxLength)
        {
            var currentFilters = new List <IInputFilter>(editText.GetFilters() ?? new IInputFilter[0]);
            var changed        = false;

            for (var i = 0; i < currentFilters.Count; i++)
            {
                if (currentFilters[i] is InputFilterLengthFilter)
                {
                    currentFilters.RemoveAt(i);
                    changed = true;
                    break;
                }
            }

            if (maxLength > 0)
            {
                currentFilters.Add(new InputFilterLengthFilter(maxLength));
                changed = true;
            }

            if (changed)
            {
                editText.SetFilters(currentFilters.ToArray());
            }

            var newText = editText.Text.TrimToMaxLength(maxLength);

            if (editText.Text != newText)
            {
                editText.Text = newText;
            }
        }
示例#2
0
        public static void UpdateMaxLength(this AppCompatEditText editText, int maxLength)
        {
            var currentFilters = new List <IInputFilter>(editText.GetFilters() ?? new IInputFilter[0]);

            for (var i = 0; i < currentFilters.Count; i++)
            {
                if (currentFilters[i] is InputFilterLengthFilter)
                {
                    currentFilters.RemoveAt(i);
                    break;
                }
            }

            currentFilters.Add(new InputFilterLengthFilter(maxLength));

            editText.SetFilters(currentFilters.ToArray());

            editText.Text = TrimToMaxLength(editText.Text, maxLength);
        }