我有一个MultiAutoCompleteTextView
,而且我正在传递国家/地区名称,这样就行了,用户可以选择不同的国家/地区。
但我想为每个文本添加样式,例如以下
我已经研究了很多解决方案,例如
https://github.com/splitwise/TokenAutoComplete
https://github.com/shanempope/TokenAutoCompleteXamarinBinding(C#Binding)
但我的要求很简单,我只想为MultiAutoCompleteTextView
中的每个文本设置样式 - 是否可以不使用外部库?
答案 0 :(得分:1)
我已经完成了一些事情,你可以尝试一下:
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>