Android 开发:如何用 Spanable 替换部分 EditText [英] Android Development: How To Replace Part of an EditText with a Spannable
问题描述
我正在尝试用 span 替换从 getText()
返回的 Editable
的一部分.
I'm trying to replace part of an Editable
returned from getText()
with a span.
我试过 getText().replace()
但这仅适用于 CharSequences
.
I've tried getText().replace()
but that's only for CharSequences
.
我尝试这样做的原因是我可以一个接一个地突出显示 EditText
的部分(经过一小段延迟后),而不是通过并突出显示整个 EditText
(对于大文件可能会很慢).
The reason I'm trying to do this is so I can highlight sections of an EditText
one after another (after a small delay), instead of going through and highlighting the whole EditText
(which can be slow with big files).
有人知道我将如何做这件事吗?
Does anyone have a clue about how I'd go about doing this?
推荐答案
这个最小尺寸的例子使first"这个词变大:
This minimal size example makes the word 'first' large:
public class SpanTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String dispStr = "I'm the first line
I'm the second line";
TextView tv = (TextView) findViewById(R.id.textView1);
int startSpan = dispStr.indexOf("first");
int endSpan = dispStr.indexOf("line");
Spannable spanRange = new SpannableString(dispStr);
TextAppearanceSpan tas = new TextAppearanceSpan(this, android.R.style.TextAppearance_Large);
spanRange.setSpan(tas, startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(spanRange);
}
}
您可以根据自己的需要进行调整
You can adapt it to your needs
这篇关于Android 开发:如何用 Spanable 替换部分 EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!