Wol*_*ter 7 java android highlight android-edittext
我有以下问题:我试图EditText通过调用同时突出显示多个控件中的文本viewXYZ.setSelection(int, int),但选择仅在焦点视图上可见.
有没有办法绕过这个,突出显示未聚焦的文本EditText?也许通过重载onDraw()方法?
我知道,但它(据我所知?)是在 EditText 控件中标记文本的唯一方法。
EditText 支持Spannable对象,因此您可以自己将突出显示应用于文本(例如背景颜色)。
此示例项目演示了一个搜索字段,该搜索字段根据搜索结果将背景颜色应用于较大的文本片段。关键部分是searchFor()方法:
private void searchFor(String text) {
TextView prose=(TextView)findViewById(R.id.prose);
Spannable raw=new SpannableString(prose.getText());
BackgroundColorSpan[] spans=raw.getSpans(0,
raw.length(),
BackgroundColorSpan.class);
for (BackgroundColorSpan span : spans) {
raw.removeSpan(span);
}
int index=TextUtils.indexOf(raw, text);
while (index >= 0) {
raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
+ text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index=TextUtils.indexOf(raw, text, index + text.length());
}
prose.setText(raw);
}
Run Code Online (Sandbox Code Playgroud)
但请注意,您的“输出字符串”可能应该是 a TextView,而不是EditText. EditText用于输入,而不是输出。
| 归档时间: |
|
| 查看次数: |
708 次 |
| 最近记录: |