Android 如何在AutoCompleteTextView中设置每个单词的文本颜色
我有一个带有自定义适配器的AutoCompleteTextView。我想要的是,当我在搜索框中键入文本时,我只想将文本颜色设置为建议中包含文本的单词,而不是整个建议。我该怎么做呢。下面是我的自定义适配器Android 如何在AutoCompleteTextView中设置每个单词的文本颜色,android,textview,autocompletetextview,Android,Textview,Autocompletetextview,我有一个带有自定义适配器的AutoCompleteTextView。我想要的是,当我在搜索框中键入文本时,我只想将文本颜色设置为建议中包含文本的单词,而不是整个建议。我该怎么做呢。下面是我的自定义适配器 public class SearchAutoComplectAdapter extends ArrayAdapter<TagBo> { Context context; LayoutInflater inflate; private VenueFilter
public class SearchAutoComplectAdapter extends ArrayAdapter<TagBo> {
Context context;
LayoutInflater inflate;
private VenueFilter filter;
public SearchAutoComplectAdapter(Context context, int resource) {
super(context, android.R.layout.simple_list_item_1);
this.context = context;
inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflate.inflate(R.layout.search_item, parent, false);
}
TextView venueName = (TextView) convertView
.findViewById(R.id.search_item_venue_name);
venueName.setTextColor(context.getResources().getColor(R.color.cyan));
TextView venueAddress = (TextView) convertView
.findViewById(R.id.search_item_venue_address);
TagBo recordBo=getItem(position);
// TODO add shared With Me check
if(recordBo.getTagTypeId() == 3){
venueName.setText(recordBo.getName());
venueAddress.setVisibility(View.GONE);
}else{
venueName.setText(recordBo.getName());
venueAddress.setText(recordBo.getSuggestText());
venueAddress.setVisibility(View.VISIBLE);
}
return convertView;
}
@Override
public Filter getFilter() {
if (filter == null) {
filter = new VenueFilter();
}
return filter;
}
private class VenueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults result = new FilterResults();
if(constraint!=null ){
List<TagBo> matchingTags = TagDao.getMatchingTags(constraint.toString());
StringBuilder text = new StringBuilder();
for (TagBo tag : matchingTags) {
text.append(tag.getSuggestText() + "\n");
}
result.values = matchingTags;
result.count = matchingTags.size();
}else{
result.count = 0;
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// we clear the adapter and then pupulate it with the new results
clear();
if (results.count > 0) {
for (TagBo o : (ArrayList<TagBo>) results.values) {
add(o);
}
notifyDataSetChanged();
}
}
}
}
公共类searchAutoCompletAdapter扩展了ArrayAdapter{
语境;
更平坦的充气;
专用维纳滤波器;
公共搜索自动完成适配器(上下文,int资源){
super(context,android.R.layout.simple\u list\u item\u 1);
this.context=上下文;
充气=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
}
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=充气。充气(R.layout.search\u项,父项,false);
}
TextView VenuName=(TextView)convertView
.findViewById(R.id.search\u item\u Vince\u name);
setTextColor(context.getResources().getColor(R.color.cyan));
TextView-venueAddress=(TextView)convertView
.findViewById(R.id.search\u item\u vention\u address);
TagBo recordBo=getItem(位置);
//TODO添加与我共享检查
if(recordBo.getTagTypeId()==3){
venueName.setText(recordBo.getName());
venueAddress.setVisibility(View.GONE);
}否则{
venueName.setText(recordBo.getName());
venueAddress.setText(recordBo.getSuggestText());
venueAddress.setVisibility(View.VISIBLE);
}
返回视图;
}
@凌驾
公共过滤器getFilter(){
if(filter==null){
过滤器=新的VenueFilter();
}
回流过滤器;
}
私有类VenueFilter扩展了过滤器{
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
FilterResults结果=新的FilterResults();
if(约束!=null){
List matchingTags=TagDao.getMatchingTags(constraint.toString());
StringBuilder text=新的StringBuilder();
用于(TagBo标记:匹配标记){
text.append(tag.getSuggestText()+“\n”);
}
result.values=匹配标签;
result.count=matchingTags.size();
}否则{
result.count=0;
}
返回结果;
}
@抑制警告(“未选中”)
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
//我们清除适配器,然后使用新结果对其进行筛选
清除();
如果(results.count>0){
for(TagBo o:(ArrayList)results.values){
添加(o);
}
notifyDataSetChanged();
}
}
}
}
也许您需要使用SpannableString作为setText()的参数;
您可以使用mSpannableString.setSpan(新背景(颜色))自定义字符串中的每个单词,我认为您需要为此创建自定义适配器类,然后您可以将
textview
设置为类似的值。
因此,将膨胀的布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="44dip"
android:gravity="center_vertical"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginRight="12dip"
android:textStyle="bold"
android:textColor="#cc3333"
android:id="@+id/textview"
android:textSize="14sp" />
在适配器类中,您需要执行以下操作:
@Override
public View getView(int position, View v, ViewGroup parent)
{
View mView = v ;
if(mView == null){
LayoutInflater vi = (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(id, null);
}
TextView text = (TextView) mView.findViewById(R.id.textview);
if(getItem(position) != null )
{
text.setTypeface(faceBold);
if(getItem(position).equals("check some condition if you want. "))
{
text.setTextColor(Color.parseColor("#999999"));
text.setText("set some text");
}
else
{
text.setTextColor(Color.parseColor("#cc3333"));
text.setText(getItem(position));
}
}
return mView;
}
享受您的代码时间:)使用spannable string ref this@adityaswant,如果任何答案对您有帮助,请不要忘记接受和/或投票。好的,您的解决方案看起来很有说服力。您能告诉我如何将我在搜索框中键入的文本传递给此方法吗?我的意思是,我正在尝试使用WhatsApp功能,当我们在搜索中键入名称时,会出现建议,但只有特定的文本是彩色的。好的,我知道了。很多
@Override
public View getView(int position, View v, ViewGroup parent)
{
View mView = v ;
if(mView == null){
LayoutInflater vi = (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(id, null);
}
TextView text = (TextView) mView.findViewById(R.id.textview);
if(getItem(position) != null )
{
text.setTypeface(faceBold);
if(getItem(position).equals("check some condition if you want. "))
{
text.setTextColor(Color.parseColor("#999999"));
text.setText("set some text");
}
else
{
text.setTextColor(Color.parseColor("#cc3333"));
text.setText(getItem(position));
}
}
return mView;
}