如何在Textview中搜索单词? [英] How Do I Search For A Word In A Textview?
问题描述
好吧,我有一个EditText
,一个Search Button
和一个Textview
带有可滚动的大文本.
Ok, I have an EditText
, A Search Button
And A Textview
with a large scrollable text.
当我在EditText
中输入一个单词并按Search Button
时,该单词将突出显示.直到这一切都正常了.
但是我想要的是,当我输入一个单词并按下button
时,它应该突出显示该单词并将相机移至该特定单词(我认为它在android中称为focusing),我不想滚动每个是时候找到我突出显示的单词了.假设我键入的单词在文本的底部并突出显示,我每次都无法滚动找到它,这很烦人.所以请帮忙!
When I enter a word in EditText
and press the Search Button
, the word gets highlighted.Till this, it all works fine.
But what I want is that when I enter a word and press the button
, it should highlight the word and also move the camera to that particular word ( I think its called focusing in android), I don't want to scroll every time to find my highlighted word.Say my typed word is at the bottom of the text and is highlighted, I can't scroll it every time to find it, it's very irritating.So please help!
所以我想要的是,当我搜索一个单词时,它应该会 突出显示,并且也应该自动出现在屏幕上,而不会 我向下滚动或向上滚动.
So what I want is that when I search for a word it should get highlighted and should also appear on the screen automatically without me scrolling down or up.
还有一件事情,我想在EditText时立即取消突出显示该单词 变成空的
And One More thing, I want to unhighlight the word as soon as EditText becomes empty
这是我的代码
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.xeoh.android.texthighlighter.TextHighlighter;
public class MainActivity extends AppCompatActivity {
EditText search;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = (EditText)findViewById(R.id.search);
textView = (TextView) findViewById(R.id.text);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TextHighlighter()
.setBackgroundColor(Color.parseColor("#FFFF00"))
.setForegroundColor(Color.RED)
.addTarget(textView)
.highlight(search.getText().toString(),TextHighlighter.BASE_MATCHER);
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search"
android:hint="Search"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search"
android:id="@+id/button"/>
<TextView
android:layout_width="match_parent"
android:id="@+id/text"
android:textSize="25sp"
android:layout_height="wrap_content"
android:text="@string/test"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
推荐答案
假设您拥有:
布局:
<Button
android:id="@+id/button"
android:text="search!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ScrollView
android:id="@+id/textViewWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/scrollableText"
android:textIsSelectable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
然后在您的代码中:
scrollableText = (TextView) findViewById(R.id.scrollableText);
editText = (EditText) findViewById(R.id.editText);
textViewWrapper = (ScrollView) findViewById(R.id.textViewWrapper);
button = (Button) findViewById(R.id.button);
scrollableText.setText(R.string.longText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String criteria = editText.getText().toString();
String fullText = scrollableText.getText().toString();
if (fullText.contains(criteria)) {
int indexOfCriteria = fullText.indexOf(criteria);
int lineNumber = scrollableText.getLayout().getLineForOffset(indexOfCriteria);
String highlighted = "<font color='red'>"+criteria+"</font>";
fullText = fullText.replace(criteria, highlighted);
scrollableText.setText(Html.fromHtml(fullText));
textViewWrapper.scrollTo(0, scrollableText.getLayout().getLineTop(lineNumber));
}
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() == 0) {
String fullText = scrollableText.getText().toString();
fullText = fullText.replace("<font color='red'>", "");
fullText = fullText.replace("</font>", "");
scrollableText.setText(fullText);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
其中,longText
是在strings.xml
中定义的一些长文本.您可能要在文本上应用其他样式(就像我对<font color='red'>
所做的那样).
where longText
is some long text defined in your strings.xml
. You may want to apply some other style on your text (as I did with <font color='red'>
).
它应该做的工作.我已经在android 8(Oreo)上对其进行了测试,并且效果很好.
It should do the job. I've tested it on android 8 (Oreo) and it worked fine.
这篇关于如何在Textview中搜索单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!