How to change color of 1 word in Array list item

时间:2018-03-25 19:41:46

标签: android

For example I have an arraylist and an Item in it ("Some important words"), and I need to change color only on the word "important"

1 个答案:

答案 0 :(得分:0)

  1. 首先创建一个重要单词的arraylist。

  2. 然后为您要检查的单词创建一个arraylist。

  3. 然后检查您的单词或单词列表项是否包含或匹配重要单词arraylist中的单词。请尝试以下代码。

    ArrayList Important_Word_List = new ArrayList<String>(); //create array list for important words
    Important_Word_List.add("Important Name1"); //add important words to Important_Word_List
    Important_Word_List.add("Important Name2");
    
    ArrayList MyWord_List = new ArrayList<String>(); //create array list for checking words
    MyWord_List.add("Word1"); //add words to MyWord_List, which you want to check
    MyWord_List.add("Word2");
    
    TextView textView = (TextView) findViewById(R.id.textView); //create reference for text view or widget
    
    for (int i = 0; i < MyWord_List.size(); i++) { //iterate through MyWord_List
        if (Important_Word_List.contains(MyWord_List.get(i).toString())) { //check the word is a important word or not
            textView.setTextColor(Color.parseColor("#bdbdbd")); //for important word, set specific color to textview or widget
        } else {
            textView.setTextColor(Color.parseColor("#ddbdbd")); //if not a important word, set default color
        }
    }