0
다음과 같은 문제가 있습니다 :TextView에서 두 번째로 발견 된 단어로 이동하는 방법 | ANDROID
아래 코드는 편집 텍스트에 입력 된 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