如何转到 TextView 中找到的第二个单词 | ANDROID

How to go to the second found word in TextView | ANDROID

我有以下问题:

下面的代码是一种搜索、标记和转到在 TextView 中找到的第一个在 Edittext 中输入的单词的方法。

有谁知道如果再次输入搜索按钮如何转到第二个找到的单词以及如何标记它?

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);
        }
    });
}

感谢您的帮助!

想出一个可能的解决方案,使用与您目前使用的方法类似的方法。如果知道字符串的长度及其开始和结束位置。您可以获得全文的副本,其中与找到的最后一个单词的结尾位置保持分开。这样你就可以继续对剩余的字符串执行 indexof 并继续执行直到结束。

或者另一种可能的方法是使用其字符数组并尝试查找找到的字符的顺序。如

for int i = 0; i < fulltext.length; i++; {
    if fulltext[i] == textToFind[0] && fulltext[i+1] == textToFind[1] ....{
    }
 }