将多个自定义元素设置为MultiAutoCompleteTextView:Android

前端之家收集整理的这篇文章主要介绍了将多个自定义元素设置为MultiAutoCompleteTextView:Android前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
从我的 previous帖子继续,我能够为MultiAutoCompleteTextView设置多个元素,但是我无法用自定义背景和关闭按钮包装那些项目,就像链接图片一样.

我能够用单个元素做同样的事情,但是对于多个元素,运气不好.

这是我试过的.

// set text to MultiAutoCompleteTextView

private void setTextSample(String contactName) {

    final SpannableStringBuilder sb = new SpannableStringBuilder();
    TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.textview,null);
    tv.setText(contactName);
    BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
    bd.setBounds(0,bd.getIntrinsicWidth(),bd.getIntrinsicHeight());

    sb.append(contactName + ",");
    sb.setSpan(new ImageSpan(bd),sb.length()-(contactName.length()+1),sb.length()-1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    mMultiAutoCompleteTextView.setText(sb);
}

// wrap text with custom elements

private static Object convertViewToDrawable(View view) {
  int spec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
  view.measure(spec,spec);
  view.layout(0,view.getMeasuredWidth(),view.getMeasuredHeight());
  Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(),view.getMeasuredHeight(),Bitmap.Config.ARGB_8888);
  Canvas c = new Canvas(b);
  c.translate(-view.getScrollX(),-view.getScrollY());
  view.draw(c);
  view.setDrawingCacheEnabled(true);
  Bitmap cacheBmp = view.getDrawingCache();
  Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888,true);
  view.destroyDrawingCache();
  return new BitmapDrawable(viewBmp);
}

任何帮助是极大的赞赏.

编辑:

如果我做

mMultiAutoCompleteTextView.setText(mMultiAutoCompleteTextView.getText().toString()+","+sb);

我收到了多个文本,但它们没有自定义背景.
没有得到我错的地方.

编辑:

多个元素的示例看起来像这样

解决方法

试试这个:
class MyMultiAutoCompleteTextView extends MultiAutoCompleteTextView {

    public MyMultiAutoCompleteTextView(Context context) {
        super(context);
    }

    @Override
    protected void replaceText(CharSequence text) {
        Log.d(TAG,"replaceText " + text.getClass() + " " + text);
        super.replaceText(getSpanned(text.toString()));
    }
}

private Spanned getSpanned(String name) {
    TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.test,null);
    tv.setText(name);
    SpannableStringBuilder sb = new SpannableStringBuilder(name);
    sb.setSpan(new ViewReplacementSpan(tv),sb.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;
}

class ViewReplacementSpan extends DynamicDrawableSpan {
    private View v;
    private Drawable drawable;

    public ViewReplacementSpan(View v) {
        super(ALIGN_BOTTOM);
        this.v = v;
        int spec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
        v.measure(spec,spec);
        v.layout(0,v.getMeasuredWidth(),v.getMeasuredHeight());
        drawable = new SpanDrawable();
        drawable.setBounds(0,v.getMeasuredHeight());
    }

    @Override
    public Drawable getDrawable() {
        return drawable;
    }

    class SpanDrawable extends Drawable {
        @Override
        public void draw(Canvas canvas) {
            canvas.clipRect(getBounds());
            v.draw(canvas);
        }

        @Override
        public void setAlpha(int alpha) {
        }

        @Override
        public void setColorFilter(ColorFilter cf) {
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }
    }
}

在onCreate()中使用以下内容对其进行测试:

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

MultiAutoCompleteTextView mactv = new MyMultiAutoCompleteTextView(this);
ArrayAdapter<Spanned> a = new ArrayAdapter<Spanned>(this,android.R.layout.simple_dropdown_item_1line);
String[] names = { "Jane","John","Mary","Mark" };
for (String name: names) {
    a.add(getSpanned(name));
}

Tokenizer tokenizer = new MultiAutoCompleteTextView.CommaTokenizer();
mactv.setTokenizer(tokenizer);
mactv.setAdapter(a);
mactv.setThreshold(1);
SpannableStringBuilder sb = new SpannableStringBuilder();
for (int i = 0; i < 2; i++) {
    sb.append(tokenizer.terminateToken(a.getItem(i)));
}
mactv.setText(sb);
ll.addView(mactv);

TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setText("try: Jane,John,Mary or Mark");
tv.setTextSize(32);
ll.addView(tv);
setContentView(ll);
原文链接:https://www.f2er.com/android/310711.html

猜你在找的Android相关文章