我有一个带有自定义适配器的AutoCompleteTextView。我想要的是当我在搜索框中键入文本时,我想将文本颜色仅设置为包含文本而不是整个建议的建议中的单词。我该怎么做。下面是我的自定义适配器。
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();
}
}
}
}
答案 0 :(得分:0)
也许你需要使用SpannableString作为setText()的参数; 你可以使用mSpannableString.setSpan(new Forgbackground(color))来自定义字符串中的evety单词
答案 1 :(得分:0)
我认为您需要为此创建自定义适配器类,然后您可以将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" />
并将Adapter对象设为:
autoAdapter = new AutoAdapter(this, R.layout.autocompletelistview_test1,
edittext);
在您的Adapter类中,您需要执行以下操作:
@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;
}
享受您的代码时间:)