如何进入TextView |中第二个找到的单词ANDROID
问题描述:
我有以下问题:如何进入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);
}
});
}
感谢帮助!
答
想到一个潜在的解决方案,使用目前类似的方法。如果知道了字符串的长度及其起始和结束位置。你可以有一个完整的文本的副本,你可以从发现的最后一个单词的结尾位置开始分割。这样你就可以继续对剩下的字符串做索引并继续下去直到结束。
或者潜在的另一种方法是有一个字符数组,并试图找到找到的字符顺序。如
for int i = 0; i < fulltext.length; i++; {
if fulltext[i] == textToFind[0] && fulltext[i+1] == textToFind[1] ....{
}
}
谢谢。我稍后再试这个:) – android