android – 如何在onTextChanged()中使用setSpan()来保存onTextChanged()的参数?


You are not told where the change took place because other afterTextChanged() methods may already have 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 look up from here where the span ended up.

从上面我所理解的是我需要在onTextChanged()中使用setSpan保存[CharSequence s,int start,int before,int count]并以某种方式在afterTextChanged()中检索它们.



import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;

public class AwesomeTextWatcherDeluxe implements TextWatcher {

    private RelativeSizeSpan span;
    private SpannableString spannable;

    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);

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {


    public void afterTextChanged(Editable s) {
        int beginIndex = spannable.getSpanStart(span);
        int endIndex = spannable.getSpanEnd(span);

        // Do your changes here.

        // Cleanup
        span = null;
        spannable = null;

转载注明原文:android – 如何在onTextChanged()中使用setSpan()来保存onTextChanged()的参数? - 代码日志