自定义元素设置为MultiAutoCompleteTextVie

自定义元素设置为MultiAutoCompleteTextVie

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

问题描述

限时删除!!

继续我的文章后,我可以设置多个元素到 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,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);
}

非常感谢任何帮助。



编辑:



如果我这样做

  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()++文本);
super.replaceText(getSpanned(text.toString()));
}
}

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

类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;
}
}
}

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 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);


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