How to use setSpan () in onTextChanged () to save onTextChanged () parameters?

I'm really trying to do map formatting because I'm trying to implement what Google says from the link

You are not told where the change occurred, because other afterTextChanged () methods may have already made other changes and invalidated the offsets. But if you need to know here, you can use setSpan (Object, int, int, int) in onTextChanged (CharSequence, int, int, int) to mark your place and then search from here where the range ended.

From what I understand, I need to save [CharSequence s, int start, int before, int count] using setSpan in onTextChanged () and somehow return them back to afterTextChanged ().

Question: on which object do I call setSpan () in onTextChanged () and how to get these stored values ​​in afterTextChanged ().

+5
source share
1 answer

So, I figured it out. Below is an example of barebones that can be used as a starting point.

import android.text.Editable; import android.text.SpannableString; import android.text.Spanned; import android.text.TextWatcher; import android.text.style.RelativeSizeSpan; public class AwesomeTextWatcherDeluxe implements TextWatcher { private RelativeSizeSpan span; private SpannableString spannable; @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // We can use any span here. I use RelativeSizeSpan with size 1, which makes absolutely nothing. // Perhaps there are other spans better suited for it? span = new RelativeSizeSpan(1.0f); spannable = new SpannableString(s); spannable.setSpan(span, start, start + count, Spanned.SPAN_COMPOSING); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { int beginIndex = spannable.getSpanStart(span); int endIndex = spannable.getSpanEnd(span); // Do your changes here. // Cleanup spannable.removeSpan(span); span = null; spannable = null; } } 
+3
source

Source: https://habr.com/ru/post/1204782/


All Articles