如何搜索活动android内的所有textviews?

时间:2016-06-13 10:21:54

标签: android

我想搜索用户在activity.can中当前存在于textview中的所有内容中输入的字符串。您提供的方式可以在最短的时间内完成。

2 个答案:

答案 0 :(得分:2)

TextView是一个View,它只显示来自某些数据源的数据。如果用户插入了一些数据,那么该数据必须被存储"在某些数据变量和视图中应该从该数据变量填充。 在这种情况下,您只需搜索数据变量。

int userAge = Integer.parseInt(editText.getText())
...
textView.setText("User age is "+userAge);

所以,数据是变量,而不是视图,你可以比较,搜索,改变它......

答案 1 :(得分:1)

userTextView.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        // search in activity textviews if contain userTextView text
        if (textView1.getText().toString().contains(userTextView.getText()))
            // content found in textView 1
        else if (textView2.getText().toString().contains(userTextView.getText()))
            // content found in textView 2
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
});