EditText android中特定于Highlite的单词
如何突出用户在EditText中选择的特定单词。例如,我有一个EditText,用户在其中输入句子“你好吗?”,现在用户可以选择单词。我必须在TextView的整个句子中显示所选的单词 我已经提到了,但还不明白。请帮助我。像这样使用html:EditText android中特定于Highlite的单词,android,android-edittext,Android,Android Edittext,如何突出用户在EditText中选择的特定单词。例如,我有一个EditText,用户在其中输入句子“你好吗?”,现在用户可以选择单词。我必须在TextView的整个句子中显示所选的单词 我已经提到了,但还不明白。请帮助我。像这样使用html: Spanned s = Html.fromHtml(string.replace(selectedString, "<font color=\"red\">" + selectedString + "</font>")); ed
Spanned s = Html.fromHtml(string.replace(selectedString, "<font color=\"red\">" + selectedString + "</font>"));
edittext.text(s);
span s=Html.fromHtml(string.replace(selectedString,“+selectedString+”);
编辑文本。文本;
您可以使用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);
}
}
}
});
yourEditText.setOnFocusChangedListener(新的OnFocusChangedListener(){
@凌驾
public void onFocusChange(视图v,布尔hasFocus){
如果(hasFocus){
if(str.contains(“你好吗?”)==true)
{
str=((EditText)v)getText.ToString().replaceAll(“你好吗?”,“你好吗?”);
((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); }
}
}
}
});
yourEditText.setOnFocusChangedListener(新的OnFocusChangedListener(){
@凌驾
public void onFocusChange(视图v,布尔hasFocus){
如果(hasFocus){
//但是ArrayList中的单词然后使用它们
用于(字符串s:arrLst){
如果(str.contains)=true{
str=((EditText)v)getText.ToString().replaceAll(“你好吗?”);
((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);
}
}
});
yourEditText.setOnFocusChangedListener(新的OnFocusChangedListener(){
@凌驾
public void onFocusChange(视图v,布尔hasFocus){
如果(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(ssString,“+ssString+”);
((EditText)v).setText(Html.fromHtml(str),TextView.BufferType.SPANNABLE);
}
}
});
试试看,这可能会对您有所帮助
"android:textColorHighlight="#00f000"
您可以使用Spanable来实现这一点 查看
EditText
有一个api可供选择:
您可以使用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.
}
});
应该给你想要的效果
结束编辑检测代码如下:我如何知道用户的selectedString?int startSelection=et.getSelectionStart();int endSelection=et.getSelectionEnd();String selectedString=String.substring(开始选择,结束选择);您可以直接:str=“font COLOR=\”RED\”>“+yourString”textdisplayedbooth.setText(Html.fromHtml(str));s2可以是任何单词,例如“你好吗?”?“您将决定要更改的单词,您可以将其用作s2。我编辑了我的回答,但word不是fix用户可以选择他/她想要highlite的单词。您将要更改的单词存储在哪里?你可以把它们放在一个arraylist中,然后使用每个你可以更改它们。不。实际上,我的要求是用户在edittext中输入字符串,现在他可以在按下OK按钮后选择他喜欢highlite的单词。我想用highlited在textview中显示整个字符串和所选单词。谢谢回复。但我不想突出显示整个字符串。只有用户在edittext中选择的特定字符串。我已经使用了它,它不是整个字符串,它只用于所选单词你好,谢谢回复。。。当我在edittext中输入文本时,选择文本并按ok,它不会显示所选字符串。请帮帮我。