将多个自定义元素设置为

将多个自定义元素设置为

本文介绍了将多个自定义元素设置为 MultiAutoCompleteTextView :Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

继续我的上一篇,我能够设置多个元素到 MultiAutoCompleteTextView 但我无法像链接图片中那样用自定义背景和关闭按钮包装这些项目.

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

这是我试过的.

//设置文本为 MultiAutoCompleteTextView

private void setTextSample(String contactName) {最终的 SpannableStringBuilder sb = 新的 SpannableStringBuilder();TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.textview, null);tv.setText(contactName);BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);bd.setBounds(0, 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);}

//用自定义元素包裹文本

私有静态对象 convertViewToDrawable(View view) {int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);视图.测量(规格,规格);view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());位图 b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),Bitmap.Config.ARGB_8888);画布 c = 新画布(b);c.translate(-view.getScrollX(), -view.getScrollY());view.draw(c);view.setDrawingCacheEnabled(true);位图 cacheBmp = view.getDrawingCache();位图 viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);view.destroyDrawingCache();返回新的 BitmapDrawable(viewBmp);}

非常感谢任何帮助.

如果我这样做

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

我收到多个文本,但它们没有用自定义背景包裹.不明白我哪里出错了.

样本多个元素看起来像这样

解决方案

试试这个:

class MyMultiAutoCompleteTextView 扩展 MultiAutoCompleteTextView {公共 MyMultiAutoCompleteTextView(上下文上下文){超级(上下文);}@覆盖protected void replaceText(CharSequence text) {Log.d(TAG, "replaceText " + text.getClass() + " " + text);super.replaceText(getSpanned(text.toString()));}}私人跨区 getSpanned(字符串名称){TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.test, null);tv.setText(名称);SpannableStringBuilder sb = new SpannableStringBuilder(name);sb.setSpan(new ViewReplacementSpan(tv), 0, sb.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);返回某人;}类 ViewReplacementSpan 扩展了 DynamicDrawableSpan {私人视图 v;私有 Drawable drawable;公共 ViewReplacementSpan(View v) {超级(ALIGN_BOTTOM);this.v = v;int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);v.measure(规格,规格);v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());drawable = new SpanDrawable();drawable.setBounds(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());}@覆盖公共可绘制 getDrawable() {返回可绘制;}类 SpanDrawable 扩展 Drawable {@覆盖公共无效绘制(帆布画布){canvas.clipRect(getBounds());v.draw(画布);}@覆盖公共无效 setAlpha(int alpha) {}@覆盖公共无效 setColorFilter(ColorFilter cf) {}@覆盖公共 int getOpacity() {返回 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(字符串名称:名称){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 

Continuing from my previous post, I was able to set multiple elements to the MultiAutoCompleteTextView but I was not able to wrap those items with custom background and close button as in that link picture.

I was able to do the same with single element but for multiple, ran out of luck.

This is what I tried.

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, 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);
}
private static Object convertViewToDrawable(View view) {
  int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
  view.measure(spec, spec);
  view.layout(0, 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);
}

Any help is greatly appreciated.

Edit :

If I do

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

I am getting multiple texts but they are not wrapped with custom background.Not getting where I am going wrong.

Edit :

Sample multiple elements would look something like this

解决方案

try this:

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), 0, 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, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
        drawable = new SpanDrawable();
        drawable.setBounds(0, 0, v.getMeasuredWidth(), 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;
        }
    }
}

test it with the following in 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);

这篇关于将多个自定义元素设置为 MultiAutoCompleteTextView :Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:13