我正在开发模块以从编辑文本中搜索抓取文本并将其搜索到当前文本视图中。如果存在,则在当前文本视图中突出显示此文本。我也用谷歌搜索了这段代码,但没有找到任何相关的答案。
tv=(TextView) mView.findViewById(R.id.detailsText);
edit_text=(EditText)findViewById(R.id.searchText);
edit_text.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
hightLightText(tv, s.toString());
}
});
}
void hightLightText(TextView textView, String searchString){
try{
String s=getResources().getString(R.string.firstpage);
String withHighLightedText = s.replaceAll(searchString, "<font color='red'>"+searchString+"</font>");
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(withHighLightedText), TextView.BufferType.SPANNABLE);
}catch(Exception ex){
}
}