Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在文本视图中搜索单词?_Android_Search_Android Edittext_Textview - Fatal编程技术网

Android 如何在文本视图中搜索单词?

Android 如何在文本视图中搜索单词?,android,search,android-edittext,textview,Android,Search,Android Edittext,Textview,好的,我有一个EditText、一个Search按钮和一个带有大的可滚动文本的Textview 当我在编辑文本中输入一个单词并按下搜索按钮时,该单词将突出显示。在此之前,一切正常。 但我想要的是,当我输入一个单词并按下按钮时,它应该高亮显示该单词,并将相机移动到该特定单词(我认为它在android中称为聚焦),我不想每次滚动都找到高亮显示的单词。假设我键入的单词位于文本底部并高亮显示,我不能每次滚动都能找到它,这很烦人。所以请帮忙 所以我想要的是,当我搜索一个单词时,它应该 突出显示,也应自动出

好的,我有一个
EditText
、一个
Search按钮
和一个带有大的可滚动文本的
Textview

当我在
编辑文本
中输入一个单词并按下
搜索按钮
时,该单词将突出显示。在此之前,一切正常。
但我想要的是,当我输入一个单词并按下
按钮时,它应该高亮显示该单词,并将相机移动到该特定单词(我认为它在android中称为聚焦),我不想每次滚动都找到高亮显示的单词。假设我键入的单词位于文本底部并高亮显示,我不能每次滚动都能找到它,这很烦人。所以请帮忙

所以我想要的是,当我搜索一个单词时,它应该 突出显示,也应自动出现在屏幕上,无需 让我向下或向上滚动

还有一件事,我想在编辑文本时尽快取消高亮显示单词 变空

这是我的代码

导入android.graphics.Color;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入com.xeoh.android.texthighlighter.texthighlighter;
公共类MainActivity扩展了AppCompatActivity{
编辑文本搜索;
文本视图文本视图;
按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search=(EditText)findViewById(R.id.search);
textView=(textView)findViewById(R.id.text);
按钮=(按钮)findViewById(R.id.button);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新文本荧光笔()
.setBackgroundColor(Color.parseColor(#FFFF00”))
.setForegroundColor(颜色:红色)
.addTarget(文本视图)
.highlight(search.getText().toString()、TextHighlighter.BASE_MATCHER);
}
});
}
}

假设您有:

布局:

    <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) {

        }
    });
scrollableText=(TextView)findViewById(R.id.scrollableText);
editText=(editText)findViewById(R.id.editText);
textViewWrapper=(ScrollView)findViewById(R.id.textViewWrapper);
按钮=(按钮)findViewById(R.id.button);
scrollableText.setText(R.string.longText);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串条件=editText.getText().toString();
String fullText=scrollableText.getText().toString();
if(全文包含(标准)){
int indexOfCriteria=fullText.indexOf(标准);
int lineNumber=scrollableText.getLayout().getLineForOffset(indexOfCriteria);
突出显示的字符串=“标准+”;
全文=全文。替换(标准,突出显示);
scrollableText.setText(Html.fromHtml(全文));
textViewWrapper.scrollTo(0,scrollableText.getLayout().getLineTop(lineNumber));
}
}
});
editText.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i1,int i2){
}
@凌驾
public void onTextChanged(CharSequence CharSequence,int i,int i1,int i2){
if(charSequence.length()==0){
String fullText=scrollableText.getText().toString();
全文=全文。替换(“,”);
全文=全文。替换(“,”);
scrollableText.setText(全文);
}
}
@凌驾
public void PostTextChanged(可编辑){
}
});
其中
longText
是在
strings.xml
中定义的一些长文本。您可能希望在文本上应用其他样式(就像我在
中所做的那样)


它应该能完成任务。我已经在安卓8(Oreo)上测试过了,效果很好。

我认为用标准TextView实现这一点非常困难,如果不是不可能的话。使用唯一的长文本视图,您无法知道正在搜索的字符串的位置,也无法检索该字符串的位置以滚动到该坐标…如果我有多个文本视图,每个文本视图都有一个id,那么我是否可以按id搜索整个文本视图,并在屏幕上显示它也许我读错了@NSimon链接的答案,我不知道它是否适合你,但也许你可以尝试一下least@FurqanHussain滚动到正确位置所需的一切都由您知道如何在文本视图中搜索/定位正确单词/句子的答案提供。答案告诉你的是“首先滚动到该文本视图的位置。然后滚动到找到文本所在行的位置,给出Layout.getLineForOffset和Layout.getLineTop的结果”。@FurqanHussain不客气。请把答案标为正确答案。