如何通过SearchView或EditText在Android中查找文本?

时间:2015-04-23 14:24:06

标签: android textview find searchview textwatcher

我是Android的新手,我正在创建一个带有非常长字符串的TextView的应用程序,我希望用户能够通过SearchView或EditText搜索TextView中的特定单词。我知道这是可能的,因为我在某些应用程序中看到了它,但无法找到有关如何操作的任何示例。我读了一些关于textwatcher的内容,但没有看到怎么做。我想要的是按Ctrl + F工作:找到书面文字,应用程序选择它。 像这样:

enter image description here

1 个答案:

答案 0 :(得分:0)

你会清楚你想要如何搜索文本吗?你只是想找到它在哪里?另外,您想通过输入EditText中的每个字母进行搜索吗?

如果你只是想做两件事,那么诀窍如下:

boolean hasText=false;
editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (count > 0 && (s.charAt(0) != '1')) {
               hasText=textView.getText().toString().contains(s.toString());
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

通过检查hasText是真还是假,你不会在那里。

相关问题