gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:33 25 4
gpt4 key购买 nike

我实际上正在尝试进行卡片格式化,因为我正在尝试实现 google 从 link 中所说的内容

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() 中检索它们。

问题是,我在 onTextChanged() 中对哪个对象调用 setSpan() 以及如何在 afterTextChanged() 中检索那些保存的值。

最佳答案

所以我想到了这个。下面是一个可以用作起点的准系统示例。

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

关于android - 如何在onTextChanged()中使用setSpan()来保存onTextChanged()的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com