private void HandleIgnoredTag(bool opening, IEditable output) { const string IGNORED_ANNOTATION_KEY = "DUMMY_KEY"; const string IGNORED_ANNOTATION_VALUE = "DUMMY_VALUE"; var len = output.Length(); if (opening) { output.SetSpan(new Annotation(IGNORED_ANNOTATION_KEY, IGNORED_ANNOTATION_VALUE), len, len, SpanTypes.MarkMark); } else { var start = output.GetSpans(0, output.Length(), Java.Lang.Class.FromType(typeof(Annotation))) .Cast <Annotation>() .Where(a => output.GetSpanFlags(a) == SpanTypes.MarkMark) .Where(a => a.Key == IGNORED_ANNOTATION_KEY) .Where(a => a.Value == IGNORED_ANNOTATION_VALUE) .FirstOrDefault(); if (start != null) { var where = output.GetSpanStart(start); output.RemoveSpan(start); output.Delete(where, len); } } }
void ITextWatcher.AfterTextChanged(IEditable s) { // Handle removing // Any changes made to IEditable cause the method to be called again recursively var str = s.ToString(); // If valid, we're good if (str.Length == 0) { SetValueAndTriggerValueChanged(null); return; } if (double.TryParse(str, out double result)) { SetValueAndTriggerValueChanged(result); return; } // Currently typing out a decimal like ".2", with just the separator value should be 0 if (str.Length == 1 && str[0] == _decimalSeparator) { SetValueAndTriggerValueChanged(0); return; } // Valid formats are... // 5 // 65 // 5.2 // 65.2 // 65.0 // .2 // . (they're still typing the "2") // 5. (they're still typing the "2") // Basically, they can only have ONE separator... if they have a second separator, we remove the second one int indexOfFirstSeparator = str.IndexOf(_decimalSeparator); if (indexOfFirstSeparator != -1) { int indexOfSubsequentSeparator = str.IndexOf(_decimalSeparator, indexOfFirstSeparator + 1); if (indexOfSubsequentSeparator != -1) { // After this, AfterTextChanged will be called again recursively so that'll delete even more separators if there were more s.Delete(indexOfSubsequentSeparator, indexOfSubsequentSeparator + 1); } } }
public void AfterTextChanged(IEditable s) { // Remove spacing char if (s.Length() > 0 && (s.Length() % 5) == 0) { char c = s.CharAt(s.Length() - 1); if (space == c) { s.Delete(s.Length() - 1, s.Length()); } } // Insert char where needed. if (s.Length() > 0 && (s.Length() % 5) == 0) { char c = s.CharAt(s.Length() - 1); // Only if its a digit where there should be a space we insert a space if (Character.IsDigit(c) && TextUtils.Split(s.ToString(), space.ToString()).Length <= 3) { s.Insert(s.Length() - 1, space.ToString()); } } }
private void Format(IEditable text) { if (text.Length() > 0) { for (int i = text.Length(); i > 0; i--) { if (!Character.IsDigit(text.CharAt(i - 1)) || ((deleting && i == start) && (isFormatChar(i)))) { text.Delete(i - 1, i); } } for (int i = 0; i < GetStringEnd(text); i++) { if (isFormatChar(i)) { text.Insert(i, Java.Lang.String.ValueOf(format[i])); } } } }
public void AfterTextChanged(IEditable s) { editStart = mEditText.SelectionStart; editEnd = mEditText.SelectionEnd; // 先去掉监听器,否则会出现栈溢出 mEditText.RemoveTextChangedListener(this); // 注意这里只能每次都对整个EditText的内容求长度,不能对删除的单个字符求长度 // 因为是中英文混合,单个字符而言,calculateLength函数都会返回1 while (calculateLength(s) > MAX_COUNT) { // 当输入字符个数超过限制的大小时,进行截断操作 s.Delete(editStart - 1, editEnd); editStart--; editEnd--; } // mEditText.setText(s);将这行代码注释掉就不会出现后面所说的输入法在数字界面自动跳转回主界面的问题了,多谢@ainiyidiandian的提醒 mEditText.SetSelection(editStart); // 恢复监听器 mEditText.AddTextChangedListener(this); setLeftCount(); }