Android 在 TextView 或 WebView 中突出显示文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2120035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Highlight Text in TextView or WebView
提问by Jorgesys
Is it possible to Highlight text in a TextView
or WebView
?
是否可以在 aTextView
或 中突出显示文本WebView
?
I see it is possible in a EditText
我看到这是可能的 EditText
I'd like to do the same in TextView
or WebView
.
Thats Possible?
我想在TextView
or 中做同样的事情WebView
。
那可能吗?
回答by Jorgesys
enable TextView's Spannable storage! by default Spannable storage in EditText is true.
启用 TextView 的 Spannable 存储!默认情况下,EditText 中的 Spannable 存储为 true。
so
所以
TextView myTV = (TextView)findViewById(R.id.textView1);
String textString = "StackOverFlow Rocks!!!";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTV.setText(spanText);
回答by validcat
As well you could use this one solution for WebView
. Call findAllAsync
您也可以将这个解决方案用于WebView
. 称呼findAllAsync
webview.findAllAsync(((EditText) findViewById(R.id.edit)).getText().toString());
than add FindListener
to WebView
比添加FindListener
到WebView
webview.setFindListener(new FindListener() {
@Override
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
Toast.makeText(getApplicationContext(), "Matches: " + numberOfMatches, Toast.LENGTH_LONG).show();
}
});
Iterate in result with webview.findNext(false);
where false/true shows the direction.
迭代结果webview.findNext(false);
,false/true 显示方向。
But this solution was added in API level 16
!!! Instead of you can setup JavaScript for higlihting - http://www.nsftools.com/misc/SearchAndHighlight.htm
但是这个解决方案被添加进来了API level 16
!!!而不是您可以设置 JavaScript 以进行高亮 - http://www.nsftools.com/misc/SearchAndHighlight.htm
回答by Ruobin Wang
Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false.
实际上,您不必自己开发此功能。您只需要使用 EditText 而不是 TextView,同时将 EditText 的 android:editable 设置为 false。
My answer is here, hope it may help you:
我的回答就到这里,希望可以帮到你: