如何在Textview中搜索单词?

时间:2017-10-05 09:46:13

标签: android search android-edittext textview

好的,我有EditText,A Search 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);


            }
        });













    }
}

<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

假设你有:

布局:

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

        }
    });

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

它应该做的工作。我已经在android 8(奥利奥)上测试了它,它运行良好。

相关问题