突出显示在搜索的ListView项目文本

突出显示在搜索的ListView项目文本

本文介绍了突出显示在搜索的ListView项目文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想是这样的屏幕截图

I want look like this screen shot

我有搜索项目由列表视图使用自定义适配器现在我想大胆信什么我输入这里是$ C $下搜索查看

I have searched item from listview using custom adapter now i want to make bold letter what i typedHere is the code for SearchView

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.actionbar_menu_item, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));
    searchView.setOnQueryTextListener(this);
        return super.onCreateOptionsMenu(menu);
        }

public boolean onQueryTextChange(String newText) {
    // this is adapter that will be filtered
      if (TextUtils.isEmpty(newText)){
            lvCustomList.clearTextFilter();
      }
      else{
            lvCustomList.setFilterText(newText.toString());
       }
    return false;
 }

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

我要搜索的信粗体文字请帮助即时通讯新Android版

i want to bold text of search letter please help im new for android

推荐答案

我假设你有自定义适配器getCount将() getView ()实施并已筛选项目,你只需要大胆的一部分。

I assume that you have a custom Adapter with getCount() and getView() implemented and already filtering items, and you just need the bold part.

要做到这一点,你需要使用<$c$c>SpannableString,它基本上与标记连接的文本。例如,一<$c$c>TextAppearanceSpan可以用来改变字体,字体样式,大小和颜色。

To achieve that, you need to use a SpannableString, which is basically text with markup attached. For example, a TextAppearanceSpan can be used to change typeface, font style, size, and color.

所以,你应该更新您的适配器 getView()来改变,其中使用部分 textView.setText()到的东西或多或少这样的:

So, you should update your adapter's getView() to change the part where you use textView.setText() into something more or less like this:

String filter = ...;
String itemValue = ...;

int startPos = itemValue.toLowerCase(Locale.US).indexOf(filter.toLowerCase(Locale.US));
int endPos = startPos + filter.length();

if (startPos != -1) // This should always be true, just a sanity check
{
    Spannable spannable = new SpannableString(itemValue);
    ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
    TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);

    spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannable);
}
else
    textView.setText(itemValue);

这篇关于突出显示在搜索的ListView项目文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:40