在android中对象搜索而不是列表视图的字符串列表

时间:2013-09-27 06:38:23

标签: android list listview search autocomplete

当我给出从对象到适配器的字符串列表时,它只搜索列表中的那些名称并显示它们(我需要搜索总对象)。

但它显示了一个单独的视图,如下拉菜单,但我需要像这样显示它们 Search view

当我输入一个字母时,它会取出所有单词并在下拉列表中显示它们,而不是像我上面那样需要它。

要在下拉列表中显示它们,我使用了此代码

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
String[] cities= getResources().getStringArray(R.array.cities_array);
ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, cities);
textView.setAdapter(adapter);

但是如何在listview中直接高亮一个单词如图所示

提前致谢

1 个答案:

答案 0 :(得分:0)

如果您只想设置文字样式

,则需要使用SpannableString

这是我在某些应用中使用的util函数来设置文本的颜色,您可以通过将其更改为BackgroundColorSpan来修改它

如果你想做一个与绘画完全相似的东西(带有阴影的圆形绿色形状,你可以使用DynamicDrawableSpan

见这里:http://developer.android.com/reference/android/text/style/DynamicDrawableSpan.html

/** 
 * Taken with permission from PocketPermissions, this method is licensed under Apache 2.0
 * Sets two colors in a single TextView or Button.
 */
public static void setTwoColorsTextStrings( String str1, String str2, TextView tv, Context c, int color1, int color2 )
{           
    SpannableString spnstr1 = new SpannableString( str1 );
    SpannableString spnstr2 = new SpannableString( str2 );

    int start = 0;
    int len = str1.length();

    int start2 = 0;
    int len2 = str2.length();

    spnstr1.setSpan( new ForegroundColorSpan( color1 ), start, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );           
    spnstr2.setSpan( new ForegroundColorSpan( color2 ), start2, len2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );

    SpannableString fullString = SpannableString.valueOf( TextUtils.concat( spnstr1,  spnstr2 ) );

    tv.setText( fullString, BufferType.SPANNABLE );         
}