代码之家  ›  专栏  ›  技术社区  ›  Ali Ha Quang

在多个自动完成文本视图中设置每个标记的样式

  •  0
  • Ali Ha Quang  · 技术社区  · 7 年前

    我有一个 MultiAutoCompleteTextView 我通过国家的名字,这个工程可以,用户可以选择不同的国家。

    enter image description here

    但是我想为每个文本添加样式,如下所示

    enter image description here

    我已经研究了很多解决方案,比如

    https://github.com/splitwise/TokenAutoComplete

    https://github.com/shanempope/TokenAutoCompleteXamarinBinding (C结合)

    但我的要求很简单,我只想为 多自动完成文本视图 -不使用外部库是否可能?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Robbit    7 年前

    我已经完成了一些事情,你可以尝试一下:

     public class MainActivity : AppCompatActivity, AdapterView.IOnItemClickListener
        {
            SpannableStringBuilder sb = new SpannableStringBuilder();
            string[] words = new string[] {
                    "word1", "word2", "word3", "word4", "word5"};
            MultiAutoCompleteTextView macTv;
            public void OnItemClick(AdapterView parent, View view, int position, long id)
            {
                sb.Append(words[position] + " ");
    
                sb.SetSpan(new RoundedBackgroundSpan(this), sb.Length() - (words[position].Length + 1), sb.Length() - 1, SpanTypes.ExclusiveExclusive);
                macTv.SetText( sb,null);
    
                macTv.SetSelection(sb.Length());
            }
    
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.activity_main);
    
                macTv = (MultiAutoCompleteTextView)this.FindViewById(Resource.Id.mac_tv);
                ArrayAdapter<string> aaStr = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleDropDownItem1Line, words);
                macTv.Adapter=aaStr;
                macTv.OnItemClickListener = this;
                macTv.SetTokenizer(new myTokenizer());
            }
    
            public class myTokenizer : Java.Lang.Object, ITokenizer
            {
                public int FindTokenEnd(ICharSequence text, int cursor)
                {
                    int i = cursor;
                    int len = text.Length();
    
                    while (i < len)
                    {
                        if (text.CharAt(i) == ' ')
                        {
                            return i;
                        }
                        else
                        {
                            i++;
                        }
                    }
    
                    return len;
                }
    
                public int FindTokenStart(ICharSequence text, int cursor)
                {
                    int i = cursor;
    
                    while (i > 0 && text.CharAt(i - 1) != ' ')
                    {
                        i--;
                    }
                    while (i < cursor && text.CharAt(i) == ' ')
                    {
                        i++;
                    }
    
                    return i;  
                }
    
                public ICharSequence TerminateTokenFormatted(ICharSequence text)
                {
                    int i = text.Length();
    
                    while (i > 0 && text.CharAt(i - 1) == ' ')
                    {
                        i--;
                    }
    
                    if (i > 0 && text.CharAt(i - 1) == ' ')
                    {
                        return text;
                    }
                    else
                    {
                        if (text is ISpanned) {
    
                            ISpannable sp = new SpannableString(text + " ");
    
                            TextUtils.CopySpansFrom((ISpanned)text, 0, text.Length(), Java.Lang.Class.FromType(typeof(object)), sp, 0);
                            return sp;
                        } else {        
                            return new SpannableString(text + " ");
                        }
                    }
    
    
                }
            }
            public class RoundedBackgroundSpan : ReplacementSpan
            {
    
                private int CORNER_RADIUS = 10;
                private Color backgroundColor = Color.White;
                private Color textColor = Color.White;
                public RoundedBackgroundSpan(Context context): base()
                {
    
                    backgroundColor =Color.Red;
                }
    
    
    
                public override void Draw(Canvas canvas, ICharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
                {
                    RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom);
                    paint.Color= backgroundColor;
                    canvas.DrawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, paint);
                    paint.Color = textColor;
                    canvas.DrawText(text, start, end, x, y, paint);
                }
    
    
                public override int GetSize(Paint paint, ICharSequence text, int start, int end, Paint.FontMetricsInt fm)
                {
                    return Java.Lang.Math.Round(paint.MeasureText(text, start, end));
                }
    
                private float measureText(Paint paint, ICharSequence text, int start, int end)
                {
                return paint.MeasureText(text, start, end);
                }
        }
    }
    

    这里是布局:

      <MultiAutoCompleteTextView
        android:id="@+id/mac_tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="1"
            />
    
    </RelativeLayout>