Android 開發(fā)中根據(jù)搜索內(nèi)容實(shí)現(xiàn)TextView中的文字部分加粗
實(shí)現(xiàn)方式?jīng)]有引入任何依賴,輕量級(jí)實(shí)現(xiàn)需求效果
最近遇到一個(gè)需求,需要做一個(gè)搜索功能。搜索的內(nèi)容需要加粗顯示。
完成了這個(gè)功能后,寫下此博客,記錄一下實(shí)現(xiàn)過(guò)程
效果圖
首先自定義一個(gè)StyleSpan,在StyleSpan里做加粗的等匹配狀態(tài)的設(shè)置
@SuppressLint("ParcelCreator") public class SearchStyleSpan extends StyleSpan { public SearchStyleSpan(int style) { super(style); } @Override public void updateDrawState(TextPaint ds) { ds.setFakeBoldText(true); //FIXME 這里還可以做其他差異性設(shè)置(修改文字大小等) super.updateDrawState(ds); } @Override public void updateMeasureState(TextPaint paint) { paint.setFakeBoldText(true); super.updateMeasureState(paint); } }
監(jiān)聽編輯框的內(nèi)容變化
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) { } @Override public void afterTextChanged(Editable editable) { checkSearchContent(mEditText.getText().toString()); } });
通過(guò)循環(huán),將TextView 里的文字根據(jù)匹配內(nèi)容分段
SpannableStringBuilder searchStyle = new SpannableStringBuilder(); int start; while (content.contains(searchContent)) { start = content.indexOf(searchContent); searchStyle.append(getBoldSpannable(content.substring(0, start + searchContent.length()), searchContent)); content = content.substring(start + searchContent.length()); } searchStyle.append(content);
將分段好的文字進(jìn)行加粗處理
int start = content.indexOf(searchContent); SpannableStringBuilder ssb = new SpannableStringBuilder(content); ssb.setSpan(new SearchStyleSpan(Typeface.NORMAL), start, start + searchContent.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
源碼地址:https://github.com/TitleZWC/BoldSpannable
以上所述是小編給大家介紹的Android 開發(fā)中根據(jù)搜索內(nèi)容實(shí)現(xiàn)TextView中的文字部分加粗,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android TextView自定義數(shù)字滾動(dòng)動(dòng)畫
- Android實(shí)現(xiàn)數(shù)字跳動(dòng)效果的TextView方法示例
- Android textview 實(shí)現(xiàn)長(zhǎng)按自由選擇復(fù)制功能的方法
- Android 中TextView的使用imageview被壓縮問(wèn)題解決辦法
- Android中TextView文本高亮和點(diǎn)擊行為的封裝方法
- Android使用TextView跑馬燈效果
- Android仿淘寶頭條基于TextView實(shí)現(xiàn)上下滾動(dòng)通知效果
- Android自定義豎排TextView實(shí)現(xiàn)實(shí)例
相關(guān)文章
Android Imageloader的配置的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android Imageloader的配置的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07Android星級(jí)評(píng)分條的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android星級(jí)評(píng)分條的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Android開發(fā)環(huán)境搭建圖文教程 親測(cè)有效!
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)環(huán)境搭建圖文教程,親自測(cè)試有效的搭建方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Mono for Android 實(shí)現(xiàn)高效的導(dǎo)航(Effective Navigation)
Android 4.0 系統(tǒng)定義了一系列的高效導(dǎo)航方式 (Effective Navigation), 主要包括標(biāo)簽、下拉列表、以及向上和返回等, 本文介紹如何用 Mono for Android 實(shí)現(xiàn)這些的導(dǎo)航方式2012-12-12Android實(shí)現(xiàn)倒計(jì)時(shí)的方案梳理
這篇文章主要介紹了Android實(shí)現(xiàn)倒計(jì)時(shí)的方案梳理,下面文章圍繞主題展開Android倒計(jì)時(shí)方案,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08Android開發(fā)之圖形圖像與動(dòng)畫(五)LayoutAnimationController詳解
LayoutAnimationController用于為一個(gè)layout里面的控件,或者是一個(gè)ViewGroup,里面的控件設(shè)置動(dòng)畫效果,感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01Android使用WindowManager構(gòu)造懸浮view
這篇文章主要為大家詳細(xì)介紹了Android使用WindowManager構(gòu)造懸浮view的具體方法,感興趣的小伙伴們可以參考一下2016-05-05Android自定義TextView實(shí)現(xiàn)文字傾斜效果
有時(shí)候Android自帶的控件無(wú)法滿足我們的某些要求,這時(shí)就需要我們自定義控件來(lái)實(shí)現(xiàn)這些功能。比如在實(shí)際開發(fā)應(yīng)用中,我們有時(shí)需要將TextView的文字傾斜一定的角度,就需要自定義TextView。下面這篇文章就給大家介紹了利用Android TextView如何實(shí)現(xiàn)文字傾斜效果。2016-11-11