android - 如何在 Textview 中搜索单词?

标签 android search android-edittext textview

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

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

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

这是我的代码

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'> 所做的那样)。

它应该完成这项工作。我已经在 Android 8 (Oreo) 上对其进行了测试,并且运行良好。

关于android - 如何在 Textview 中搜索单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46582552/

相关文章:

android - 使 Android EditText 看起来一样

android - 我在尝试运行 Android 虚拟设备时收到错误列表

android - 预估 yield admob降为0

sharepoint - 如何在 Microsoft Search Server 2008 中创建用于索引重量级格式的预处理应用程序?

algorithm - 根据给定主题查找相关词

android - 为什么我无法获取添加的单选按钮或编辑文本的值?

Android Studio - 错误膨胀类 EditText

java - 从微调器中为项目设置值

java - Android Intent 创建对象的新实例

javascript - 在 MarkLogic 中使用 REST API 搜索调用仅提取部分 JSON 文档