如何在 Textview 中搜索单词?

Fur*_*ain 3 search android textview android-edittext

好的,我有一个带有大可滚动文本的EditText, ASearch Button和 A Textview

当我输入一个词EditText并按下 时Search Button该词会突出显示。直到这个,一切正常。
但是我想要的是,当我输入一个单词并按下 时button,它应该突出显示该单词并将相机移动到该特定单词(我认为它在 android 中称为聚焦),我不想每次都滚动以查找我的高亮词。说我输入的词在文本底部并被高亮显示,我无法每次滚动找到它,很烦人。所以请帮助!

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

还有一件事,我想在 EditText 变空后立即取消突出显示该词

这是我的代码

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


            }
        });













    }
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

sza*_*i20 5

假设你有:

布局:

    <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>
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中:

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

        }
    });
Run Code Online (Sandbox Code Playgroud)

这里longText是你定义了一些长文本strings.xml。您可能希望在文本上应用一些其他样式(就像我对 所做的那样<font color='red'>)。

它应该完成这项工作。我已经在 android 8 (Oreo) 上测试过它并且运行良好。