How Do I Search For A Word In A Textview?

2020-07-19 03:22发布

Ok, I have an EditText, A Search Button And A Textview with a large scrollable text.

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.

And One More thing, I want to unhighlight the word as soon as EditText becomes empty

Here Is My Code

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>

1条回答
叛逆
2楼-- · 2020-07-19 03:48

Assuming you have:

Layout:

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

Then in your code:

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

        }
    });

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'>).

It should do the job. I've tested it on android 8 (Oreo) and it worked fine.

查看更多
登录 后发表回答