2

如何突出显示用户在 EditText 中选择的特定单词。例如,我有一个 EditText,用户在其中输入句子“你好,你好吗?”,现在用户可以选择像这张图片这样的词。而且我必须在 TextView 的整个句子中突出显示选定的单词。

我已经提到了这个这个但不明白。请帮我。

4

4 回答 4

1

您可以通过使用 replace() 方法然后 Html.fromHtml() 来做到这一点,如下所示

yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
    @Override
    public void onFocusChange(View v, boolean hasFocus){
        if (hasFocus){
             if (str.contains("Hello how are you ?") == true) 
                {                     
                    str =  ((EditText)v)getText.ToString().replaceAll("Hello how are you ?", "<font color='red'>Hello how are you ?</font>");

                     ((EditText)v).setText(Html.fromHtml(str), TextView.BufferType.SPANNABLE);
                }
        }
    }
});

[编辑 1]

 yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
        @Override
        public void onFocusChange(View v, boolean hasFocus){
            if (hasFocus){
            // But the words in an ArrayList then use them  
            for(String s : arrLst){
            if (str.contains(s) == true)                     {                     
             str =  ((EditText)v)getText.ToString().replaceAll(s, "<font     color='red'>Hello how are you ?</font>");
              ((EditText)v).setText(Html.fromHtml(str), TextView.BufferType.SPANNABLE);                                     }

            }
          }
        }
    });

[编辑 2]

 yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
        @Override
        public void onFocusChange(View v, boolean hasFocus){
            if (hasFocus){

                 int sSelection = ((EditText)v)getText.ToString().getSelectionStart(); 
                 int eSelection = ((EditText)v)getText.ToString().getSelectionEnd(); 
                 String sString = string.substring(sSelection, eSelection);

                 str =  ((EditText)v)getText.ToString().replaceAll(sString , "<font color='red'>"+sString +"</font>");
                 ((EditText)v).setText(Html.fromHtml(str), TextView.BufferType.SPANNABLE);

            }
        }
    });
于 2013-03-18T10:11:37.757 回答
1

试试这个可能对你有帮助。

"android:textColorHighlight="#00f000"
于 2013-03-18T10:21:55.253 回答
1

您可以使用 spannable 来实现

看看EditText有一个 api 来获取选择:EditText 中的选择

TextView您可以在using中设置多色文本SpannableString

所以将两者结合在这样的代码中:

edit.setOnEditorActionListener(
     new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
        actionId == EditorInfo.IME_ACTION_DONE ||
        event.getAction() == KeyEvent.ACTION_DOWN &&
        event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
    if (!event.isShiftPressed()) {
       // the user is done typing. 
//edit is the EditText
         Spannable spann = new SpannableString(edit.getText());        
      spann.setSpan(new ForegroundColorSpan(Color.BLUE), edit.getSelectionStart(),       edit.getSelectionEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     textView.setText(spann); // TextView 
       return true; // consume.
    }                
}
return false; // pass on to other listeners. 
}
 });

应该给你想要的效果

结束编辑检测代码来自这里:所以回答

于 2013-03-18T10:34:25.753 回答
0

像这样使用html:

 Spanned s = Html.fromHtml(string.replace(selectedString, "<font color=\"red\">" + selectedString + "</font>"));

edittext.text(s);
于 2013-03-18T10:09:49.883 回答