亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實現(xiàn)自動匹配關(guān)鍵字并且標(biāo)紅功能

 更新時間:2017年05月11日 15:38:39   作者:ganchuanpu  
這篇文章主要為大家詳細介紹了Android實現(xiàn)自動匹配關(guān)鍵字并且標(biāo)紅功能,單關(guān)鍵字和多關(guān)鍵字進行匹配,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android匹配關(guān)鍵字標(biāo)紅的具體代碼,供大家參考,具體內(nèi)容如下

1. 單關(guān)鍵字匹配

若只需匹配 搜索內(nèi)容  可以寫的簡單一些,代碼如下:

if (name != null && name.contains(mKeyWord)) { 
   int index = name.indexOf(mKeyWord); 
   int len = mKeyWord.length(); 
   Spanned temp = Html.fromHtml(name.substring(0, index) 
     + "<font color=#FF0000>" 
     + name.substring(index, index + len) + "</font>" 
     + name.substring(index + len, name.length())); 
 
   holder.tv_name.setText(temp); 
  } else { 
   holder.tv_name.setText(name); 
} 

上面的name是你要顯示整個item內(nèi)容,  mKeyWord 是搜索的關(guān)鍵字  holder.tv_name 是當(dāng)前textview控件 

2.多關(guān)鍵字匹配

有的時候我們做搜索的時候 是需要將用戶輸入的關(guān)鍵字在服務(wù)端做拆分,拆分為多個關(guān)鍵字去搜索。那么服務(wù)端返回數(shù)據(jù)的時候關(guān)鍵字被拆分為多個。也就是返回的是一個關(guān)鍵字數(shù)組。

這種情況我們在客戶端做  關(guān)鍵字匹配的時候就要寫一個算法,將段落中的多個關(guān)鍵字全部匹配并且標(biāo)出來。

代碼如下:

adapter里面邏輯:

/** 
 name 是item顯示內(nèi)容 當(dāng)前item顯示字符串內(nèi)容 
 keyList 是指存放 多個關(guān)鍵字的list集合 
*/ 
StringBuffer str = new StringBuffer(""); 
str = Utils.addChild(name, keyList, str); 
holder.contentTv.setText(Html.fromHtml(str.toString())); 
/** 
 * 多關(guān)鍵字查詢表紅,避免后面的關(guān)鍵字成為特殊的HTML語言代碼 
 * @param str  檢索結(jié)果 
 * @param inputs 關(guān)鍵字集合 
 * @param resStr 表紅后的結(jié)果 
 */ 
public static StringBuffer addChild(String str,List<String> inputs,StringBuffer resStr){ 
 int index=str.length();//用來做為標(biāo)識,判斷關(guān)鍵字的下標(biāo) 
 String next="";//保存str中最先找到的關(guān)鍵字 
 for (int i = inputs.size() -1 ; i>= 0;i--) { 
  String theNext=inputs.get(i); 
  int theIndex=str.indexOf(theNext); 
  if(theIndex==-1){//過濾掉無效關(guān)鍵字 
   inputs.remove(i); 
  }else if(theIndex<index){ 
   index=theIndex;//替換下標(biāo) 
   next=theNext; 
  } 
 } 
 
 //如果條件成立,表示串中已經(jīng)沒有可以被替換的關(guān)鍵字,否則遞歸處理 
 if(index==str.length()){ 
  resStr.append(str); 
 }else{ 
  resStr.append(str.substring(0,index)); 
  resStr.append("<font color='#FF0000'>"+str.substring(index,index+next.length())+"</font>"); 
  String str1=str.substring(index+next.length(),str.length()); 
  addChild(str1,inputs,resStr);//剩余的字符串繼續(xù)替換 
 } 
 return resStr; 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論