EditText搜尋關鍵字,返回結果匹配關鍵字改變顏色
自己專案 用到EditText搜尋結果關鍵字改變顏色,就研究了一下,2種方法實現,發現一個好用的工具類,在程式碼中一行呼叫這個方法,直接實現需求。
KeywordUtil.java工具類。
package com.example.administrator.replacekeyword.utils; import android.text.SpannableString; import android.text.Spanned; import android.text.style.ForegroundColorSpan; import java.util.regex.Matcher; importjava.util.regex.Pattern; /** * 專案名稱: * 類描述: * 建立人: * 建立時間:2016/7/26 15:22 * 修改人:Administrator * 修改時間:2016/7/26 15:22 * 修改備註: */ public class KeywordUtil { /** * 關鍵字高亮變色 * * @param color * 變化的色值 * @param text * 文字 * @param keyword * 文字中的關鍵字* @return */ public static SpannableString matcherSearchTitle(int color, String text, String keyword) { SpannableString s = new SpannableString(text); Pattern p = Pattern.compile(keyword); Matcher m = p.matcher(s); while (m.find()) { int start = m.start(); intend = m.end(); s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return s; } /** * 多個關鍵字高亮變色 * * @param color * 變化的色值 * @param text * 文字 * @param keyword * 文字中的關鍵字陣列 * @return */ public static SpannableString matcherSearchTitle(int color, String text, String[] keyword) { SpannableString s = new SpannableString(text); for (int i = 0; i < keyword.length; i++) { Pattern p = Pattern.compile(keyword[i]); Matcher m = p.matcher(s); while (m.find()) { int start = m.start(); int end = m.end(); s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } return s; } }
2.第二個工具類KeywordUtil.java
package com.example.administrator.replacekeyword.utils; import android.text.SpannableString; import android.text.Spanned; import android.text.style.ForegroundColorSpan; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 專案名稱: * 類描述: * 建立人: * 建立時間:2016/7/26 15:22 * 修改人:Administrator * 修改時間:2016/7/26 15:22 * 修改備註: */ public class KeywordUtil { /** * 關鍵字高亮變色 * * @param color * 變化的色值 * @param text * 文字 * @param keyword * 文字中的關鍵字 * @return */ public static SpannableString matcherSearchTitle(int color, String text, String keyword) { SpannableString s = new SpannableString(text); Pattern p = Pattern.compile(keyword); Matcher m = p.matcher(s); while (m.find()) { int start = m.start(); int end = m.end(); s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return s; } /** * 多個關鍵字高亮變色 * * @param color * 變化的色值 * @param text * 文字 * @param keyword * 文字中的關鍵字陣列 * @return */ public static SpannableString matcherSearchTitle(int color, String text, String[] keyword) { SpannableString s = new SpannableString(text); for (int i = 0; i < keyword.length; i++) { Pattern p = Pattern.compile(keyword[i]); Matcher m = p.matcher(s); while (m.find()) { int start = m.start(); int end = m.end(); s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } return s; } }
3.Main.java
package com.example.administrator.replacekeyword.activity; import android.graphics.Color; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; import com.example.administrator.replacekeyword.R; import com.example.administrator.replacekeyword.utils.KeywordUtil; import com.example.administrator.replacekeyword.utils.MyTextView; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private EditText mEditText; private MyTextView mTV; private TextView mTv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initViews(); } private void initViews() { mEditText = (EditText) findViewById(R.id.et_search2); mTV = (MyTextView) findViewById(R.id.tv_search2); mTv2 = (TextView) findViewById(R.id.tv_search3); initEvents(); } private void initEvents() { //根據輸入框輸入值的改變來過濾搜尋 mEditText.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) { //當輸入框裡面的值為空,更新為原來的列表,否則為過濾資料列表 String result = mTV.getText().toString().trim(); String editTextStr = mEditText.getText().toString().trim(); mTV.setSpecifiedTextsColor(result, editTextStr, Color.parseColor("#FF0000")); } @Override public void afterTextChanged(Editable s) { String result = mTv2.getText().toString().trim(); String editTextStr = mEditText.getText().toString().trim(); mTv2.setText(KeywordUtil.matcherSearchTitle(Color.BLUE,result,editTextStr)); } }); } }
4.activity_main2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F0F0F0" android:orientation="vertical"> <EditText android:id="@+id/et_search2" android:layout_width="match_parent" android:layout_height="80dp" android:hint="請輸入下面一句話中的幾個字" /> <com.example.administrator.replacekeyword.utils.MyTextView android:id="@+id/tv_search2" android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center_vertical" android:text="輸入關鍵字,顯示不同"/> <TextView android:id="@+id/tv_search3" android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center_vertical" android:text="輸入關鍵字,顯示不同2"/> </LinearLayout>
原始碼下載地址:http://download.csdn.net/detail/renhaijuniryfe/9586720
相關推薦
EditText搜尋關鍵字,返回結果匹配關鍵字改變顏色
自己專案 用到EditText搜尋結果關鍵字改變顏色,就研究了一下,2種方法實現,發現一個好用的工具類,在程式碼中一行呼叫這個方法,直接實現需求。 KeywordUtil.java工具類。 package com.example.administrator.replace
搜尋歷史 AutoCompleteTextView自定義匹配關鍵字匹配
最近剛開始寫部落格質量不高 並且都是一些基礎的東西 但是我還是搞了好幾個小時 也比較菜鳥還比較笨 我還是要記錄下來這心酸的過程 ! 東西比較基礎各位不要見怪 請多多批評! 思路 所以我們按照網上說的寫 如下 上程式碼直接把介面卡換成這個就ok了 package
mybatis中使用selectKey,返回結果一直是1
batis type 這就是 字段 tis 屬性 筆記 getc tty mybatis中使用selectKey,返回結果一直是1,結合這個問題,筆記一下selectKey標簽以及問題的原因 先說需求,向數據庫插入一條記錄,表的id是自增的,插入以後返回插入記錄的id 下面
【轉】jQuery 的 ajax 方法,返回結果 readyState=4 並且 status=200 時,還進 error 方法
err 前端 log 報錯 但是 tail 格式 success ajax 今天在使用jquery.ajax方法去調用後臺方法時,ajax中得參數data類型是"JSON",後臺DEBUG調試,運行正常,返回正常的結果集,但是前端一直都進到ajax的error方法,百思不得
mysql 子查詢in,返回結果集合失敗
SELECT * FROM station_quality_detail_info qa INNER JOIN station_info sinfo on qa.station_info_id = sinfo.id WHERE FIND_IN_SET(sinfo.ser
shell script中執行sql語句,返回結果中文亂碼問題——在企業開發中的解決方案
問題描述 有一個需求,需要編寫一個指令碼向遠端資料庫拉取資料並生成一個檔案。在編寫完成並執行後,發現檔案中的中文為亂碼。 指令碼中的查詢命令如下所示: QUERY_CMD="mysql -h${HOST} -u${USER} -p${PWD} -sBN " SQL="SELECT
golang gin 中介軟體,返回結果
package main import ( "net/http" "github.com/gin-gonic/gin" ) func response() gin.HandlerFunc { return func(c *gin.Context) {
shell script中執行sql語句,返回結果中文亂碼問題
問題描述 有一個需求,需要編寫一個指令碼向遠端資料庫拉取資料並生成一個檔案。在編寫完成並執行後,發現檔案中的中文為亂碼。 指令碼中的查詢命令如下所示: QUERY_CMD="mysql -h${HOST} -u${USER} -p${PWD} -sBN " SQ
JS 陣列按指定長度切割,返回一個新的陣列集合,比如按照3的倍數切割,返回結果為每三個一組
let data = [ {name:'小趙',value:'12'}, {name:'小錢',value:'12'}, {name:'小孫',value:'12'}, {name:'小李',value:'12'}, {name:'小周
URL 在瀏覽器訪問與用CURL訪問,返回結果不一致
【問題描述】 1、該URL是一個介面,在用瀏覽器訪問時,返回的是正常的JSON資料。 2、用CURL或者基於WinInet的HTTP客戶端訪問時,返回的404的網頁內容,如下圖所示: 【解決方案】 1、確認URL在使用瀏覽器訪問時,返回的是正確的資料,目的
oracle儲存過程傳入in引數,返回結果集
create or replace procedure proc_report_TEST(zhxshss in varchar2, pcursor out sys_refcursor) as begi
thinkphp 更新資料時如果未修改資料,返回結果判斷錯誤
thinkphp的CURD中,使用save方法時會出現一個奇怪的問題,即如果資料沒有更新(與原資料相同),返回值判斷為false。其實很久之前就發現了這個問題,一度以為是官方程式碼的問題,但是一直拖延到最近才想到要去解決這個問題。不得不說:“明日復明日,明日何其多。
java執行cmd命令,返回結果中文亂碼問題解決
public static void main(String[] args) { try { // 執行ping命令 Process process = Runtime.getRuntime().exec("cmd /c e:&dir"); B
輸入一個複雜連結串列(每個節點中有節點值,以及兩個指標,一個指向下一個節點,另一個特殊指標指向任意一個節點),返回結果為複製後複雜連結串列的head。
本題源自LeetCode -------------------------------------------- 1 先複製節點,讓複製節點插入節點的後面。 2 調整隨機指標,複製節點的隨機指標指向節點隨機指標的下一個節點 3 將連結串列拆分開 RandomListN
輸入一個復雜鏈表(每個節點中有節點值,以及兩個指針,一個指向下一個節點,另一個特殊指針指向任意一個節點),返回結果為復制後復雜鏈表的head。(註意,輸出結果中請不要返回參數中的節點引用,否則判題程序會直接返回空)
下一個 註意 public dom next eno style clas lin /* public class RandomListNode { int label; RandomListNode next = null; Rando
pygame 繪製移動矩形,並碰到邊界時改變顏色
import pygame,sys; from pygame.locals import * pygame.init() screen = pygame.display.set_mode((700,600)) pygame.display.set_caption("Drawi
Android 關於TextView中,設定指定部分文字改變顏色,和指定部分文字點選事件
參考文章 我想要實現的是如下這樣的效果 首先要想實現文字變色以及點選,都需要使用到SpannableStringBuilder,例項化該類也很簡單,只需將你想要處理的字串當做引數 Span
cocos creator 粒子特效中,使貼圖不改變顏色
cocos creator 粒子特效之貼圖不變色 直接貼圖, start color start color 變化範圍 End color 結束顏色的變化範圍 貼圖
js搜尋 高亮匹配關鍵字,即時展示相關資料
searchCont:function(el,flag){ var _this = this; //空格替換 el.value = el.value.re
JS正則對象 RegExp(有變量的時候使用),用來匹配搜索關鍵字(標紅)
() replace pla 匹配標簽 span sea var style his 1,平常我們寫js正則規則的時候,一般是這樣寫: var reg = /abc/; 然而,這樣寫的話,如果abc是一個變量這樣就不行,我們需要下面這種寫法: var abc =