2

我正在开发模块以从编辑文本中搜索抓取文本并将其搜索到当前文本视图中。如果存在,则在当前文本视图中突出显示此文本。我也用谷歌搜索了这段代码,但没有找到任何相关的答案。

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){

        }

}
4

3 回答 3

2

其中的HTML 标记格式TextView非常有限。

<b>Bold</b> and <i>Italic</i>

做工作,但是

<font color>

可悲的是没有。

使用Spannable来突出显示您想要的部分文本。

这是在视图中将选定文本设为斜体的示例EditText Is there any example about Spanned and Spannable text

于 2013-04-15T08:57:55.170 回答
1

调用以下函数进行突出显示。

public void hightLightText(TextView textView, String searchString) {
    String s = textView.getText().toString();
    SpannableString str = new SpannableString(s);
    if (searchString != null && !searchString.equalsIgnoreCase("")) {
        int startIndex = 0;
        while (true) {
            startIndex = s.indexOf(searchString, startIndex);

            if (startIndex >= 0) {
                str.setSpan(new BackgroundColorSpan(Color.YELLOW),
                        startIndex, startIndex + searchString.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                startIndex++;
            } else {
                break;
            }
        }
    }
    textView.setText(str);
}
于 2013-04-15T09:11:45.203 回答
0
EditText edit_text;
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edittest);
    tv=(TextView) 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, edit_text.getText().toString().trim());
        }
    });



}


void hightLightText(TextView textView, String searchString){
    try{

        String s=getResources().getString(R.string.app_name);

        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){

        }

}

在您的代码中只需更改以下方法

 @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        hightLightText(tv, edit_text.getText().toString().trim());
    }
于 2013-04-15T09:07:21.417 回答