【问题标题】:How Do I Search For A Word In A Textview?如何在 Textview 中搜索单词?
【发布时间】:2018-03-16 21:23:43
【问题描述】:

好的,我有一个EditText、一个Search Button 和一个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);


            }
        });













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

【问题讨论】:

  • 我认为使用标准的 TextView 实现这一点非常困难,如果不是不可能的话。使用唯一的长 TextView 您无法知道要搜索的字符串的放置位置,并且您无法检索该字符串的位置以滚动到该坐标...
  • 如果我有多个textview,每个textview都有一个id,我可以通过id搜索整个textview,并显示在屏幕上
  • 也许我读错了@NSimon链接的答案,我不知道它是否适合你,但也许你至少可以试一试
  • @FurqanHussain 滚动到正确位置所需的一切都由答案 stackoverflow.com/a/21055657/4232337 给出。您知道如何在 TextView(s) 中搜索/定位正确的单词/句子。答案告诉你的是“首先滚动到该 TextView 的位置。然后根据 Layout.getLineForOffsetLayout 的结果滚动到找到的文本所在行的位置。 getLineTop".

标签: android search android-edittext textview


【解决方案1】:

假设你有:

布局:

    <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 中定义的一些长文本。您可能想在文本上应用其他样式(就像我对 &lt;font color='red'&gt; 所做的那样)。

它应该可以完成这项工作。我已经在 android 8 (Oreo) 上对其进行了测试,效果很好。

【讨论】:

  • @FurqanHussain 不客气。请将答案标记为正确答案。
猜你喜欢
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多