This article shares the specific code for Android matching keyword red for your reference. The specific content is as follows
1. Single keyword matching
If you only need to match the search content, you can write it simpler, the code is as follows:
if (name != null && (mKeyWord)) { int index = (mKeyWord); int len = (); Spanned temp = ((0, index) + "<font color=#FF0000>" + (index, index + len) + "</font>" + (index + len, ())); holder.tv_name.setText(temp); } else { holder.tv_name.setText(name); }
The name above is that you want to display the entire item content, mKeyWord is the search keyword holder.tv_name is the current textview control
2. Multi-keyword matching
Sometimes when we do searching, we need to split the keywords entered by the user on the server and split them into multiple keywords for searching. Then when the server returns data, the keywords are split into multiple. That is, what is returned is a keyword array.
In this case, when we do keyword matching on the client, we need to write an algorithm to match and mark all the keywords in the paragraph.
The code is as follows:
Logic in adapter:
/** name is the content of the item. The current item displays the string content keyList refers to a list collection that stores multiple keywords. */ StringBuffer str = new StringBuffer(""); str = (name, keyList, str); ((()));
/** * Multi-keyword query table is red, to avoid the subsequent keywords becoming special HTML language code * @param str Search results * @param inputs keyword collection * @param resStr Table of red results */ public static StringBuffer addChild(String str,List<String> inputs,StringBuffer resStr){ int index=();// Used as a marker to determine the subscript of keywords String next="";//Save the first keyword found in str for (int i = () -1 ; i>= 0;i--) { String theNext=(i); int theIndex=(theNext); if(theIndex==-1){//Filter out invalid keywords (i); }else if(theIndex<index){ index=theIndex;//Replace subscript next=theNext; } } //If the condition is true, it means that there is no keyword in the string that can be replaced, otherwise recursively processed if(index==()){ (str); }else{ ((0,index)); ("<font color='#FF0000'>"+(index,index+())+"</font>"); String str1=(index+(),()); addChild(str1,inputs,resStr);//Continue to replace the remaining strings } return resStr; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.