問題描述
目前我正在開發 Spelling &語法檢查應用程序.它有 EditText 用戶可以在其中輸入文本和單擊按鈕應用程序時,一個名為檢查文本"的按鈕將調用 LanguageTool API 來檢查文本和返回帶有結果的 JSON 響應.
Currently I'm developing Spelling & Grammar checking app. It has EditText where user can input text & one button called "Check Text " upon click on button app will call LanguageTool API to check text & returns JSON response with result .
這是應用程序的屏幕截圖:
Here is screenshot of app :
這是我迄今為止嘗試突出顯示多個單詞的代碼,但此代碼僅突出顯示我創建的數組中的最后一個單詞:
Here is code which I have tried so far for highlighting multiple words but this code only highlights last word from array which I have created:
for (int i = 0; i < errorStrings.size(); i++) {
// Here textToCheck is EditText & errorStrings is ArrayList of type WrongString class which i have created to hold Error string , offset & length.
Spannable wordtoSpan = new SpannableString(texttoSend);
wordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE),errorStrings.get(i).getOffset(),
(errorStrings.get(i).getOffset()+errorStrings.get(i).getLength()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textToCheck.setText(wordtoSpan);
}
推薦答案
我寫了一個簡單的方法,可以讓你傳遞TextView
(或子類Button
、Edittext
等).
I have wrote a simple method that allow you pass TextView
(or child classes Button
, Edittext
etc.).
1.如果您想突出顯示段落中查找單詞中的文本.您可以使用以下方法.
setHighLightedText(yourTextView_Edittext_Button, "a");
這會給你這樣的結果.
/**
* use this method to highlight a text in TextView
* @param tv TextView or Edittext or Button or child of TextView class
* @param textToHighlight Text to highlight
*/
public void setHighLightedText(TextView tv, String textToHighlight) {
String tvt = tv.getText().toString();
int ofe = tvt.indexOf(textToHighlight, 0);
Spannable wordToSpan = new SpannableString(tv.getText());
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(textToHighlight, ofs);
if (ofe == -1)
break;
else {
wordToSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + textToHighlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
}
}
}
<強>2.如果您想制作可點擊的突出顯示文本(例如單擊條款和條件文本),請使用以下代碼:
setClickableHighLightedText(yourTextView_Edittext_Button, "go to settings", new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: do your stuff here
}
});
這會給你這樣的結果
/**
* use this method to set clickable highlighted a text in TextView
*
* @param tv TextView or Edittext or Button or child of TextView class
* @param textToHighlight Text to highlight
*/
public void setClickableHighLightedText(TextView tv, String textToHighlight, View.OnClickListener onClickListener) {
String tvt = tv.getText().toString();
int ofe = tvt.indexOf(textToHighlight, 0);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
if (onClickListener != null) onClickListener.onClick(textView);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(0xff0000ff);
ds.setUnderlineText(true);
}
};
SpannableString wordToSpan = new SpannableString(tv.getText());
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(textToHighlight, ofs);
if (ofe == -1)
break;
else {
wordToSpan.setSpan(clickableSpan, ofe, ofe + textToHighlight.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
這是一種解決方法,您可以根據需要自定義跨度.一些很好的教程 Android 文本樣式 和 另一個
This is a workaround, you can customise spans according to your need. Some good tutorials Android text styles and one other
這篇關于如何在 EditText 中突出顯示多個單詞?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!