0
我有以下問題:如何進入TextView |中第二個找到的單詞ANDROID
下面的代碼是搜索,標記和去找到的第一個字,其中在EDITTEXT進入TextView的方法。
有沒有人知道如何再次進入搜索按鈕,以及如何標記它,以找到第二個發現的單詞?
String textToFind = Etxt.getText().toString().trim().toLowerCase();
String fullTxt = textView.getText().toString();
SpannableString spannable = new SpannableString(fullTxt);
final int index = fullTxt.indexOf(textToFind);
if(index == -1) {
// text does not contain the word
Toast.makeText(getApplicationContext(), "Text '" + textToFind + "' not found.", Toast.LENGTH_SHORT).show();
}
else {
int lineNum = textView.getLayout().getLineForOffset(index);
int lineStart = textView.getLayout().getLineEnd(lineNum -1);
int lineEnd = textView.getLayout().getLineEnd(lineNum);
// set style to the entire line, as your origional code
spannable.setSpan(new ForegroundColorSpan(Color.parseColor("#035525")), lineStart, lineEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), lineStart, lineEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
textView.post(new Runnable() {
@Override
public void run() {
int line = textView.getLayout().getLineForOffset(index);
int y = textView.getLayout().getLineTop(line);
scrollView.scrollTo(0, y);
}
});
}
感謝幫助!
謝謝。我稍後再試這個:) – android