联系Bubble EditText

too*_*o42 89 html android drawable android-edittext

我正在尝试创建与MultiAutoCompleteTextViewGoogle+应用中的实施方式类似的联系人气泡.以下是截图:

Google+撰写帖子截图 .

我试图扩展DynamicDrawableSpan该类,以便在一段文本的背景中获得一个spannable drawable

public class BubbleSpan extends DynamicDrawableSpan {
  private Context c;

  public BubbleSpan(Context context) {
    super();
    c = context;
  }

  @Override
  public Drawable getDrawable() {
    Resources res = c.getResources();
    Drawable d = res.getDrawable(R.drawable.oval);
    d.setBounds(0, 0, 100, 20);
    return d;
  }
}
Run Code Online (Sandbox Code Playgroud)

我的oval.xml drawable定义如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <solid android:color="#352765"/>
  <padding android:left="7dp" android:top="7dp"
    android:right="7dp" android:bottom="7dp" />
  <corners android:radius="6dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

在我的Activity类中MulitAutoCompleteTextView,我设置了气泡跨度,如下所示:

final Editable e = tv.getEditableText();
final SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append("some sample text");
sb.setSpan(new BubbleSpan(getApplicationContext()), 0, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
e.append(sb); 
Run Code Online (Sandbox Code Playgroud)

但是,不是在字符串中的前6个字符后面显示的椭圆形状,而是字符不可见,并且背景中没有椭圆形可绘制.

如果我更改BubbleSpan的getDrawable()方法以使用.png而不是形状drawable:

public Drawable getDrawable() {
  Resources res = c.getResources();
  Drawable d = res.getDrawable(android.R.drawable.bottom_bar);
  d.setBounds(0, 0, 100, 20);
  return d;
}
Run Code Online (Sandbox Code Playgroud)

然后会出现.png,但字符串中作为跨度一部分的字符将不会显示.如何才能使跨度中的字符显示在前景中,同时自定义形状可绘制在后台显示?

我试图使用ImageSpan而不是子类,DynamicDrawableSpan但是不成功.

too*_*o42 55

谢谢@chrish的所有帮助.所以这就是我做到的:

final SpannableStringBuilder sb = new SpannableStringBuilder();
TextView tv = createContactTextView(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);
to_input.setText(sb);

public TextView createContactTextView(String text){
  //creating textview dynamically
  TextView tv = new TextView(this);
  tv.setText(text);
  tv.setTextSize(20);
  tv.setBackgroundResource(R.drawable.oval);
  tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_clear_search_api_holo_light, 0);
  return tv;
}

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

}
Run Code Online (Sandbox Code Playgroud)

  • 我发现由于扩展问题,这在所有设备中都不起作用.更好的方法是从convertView返回位图,然后在设置可绘制边时使用实际位图的尺寸.例如`BitmapDrawable bd = new BitmapDrawable(bitmap); bd.setBounds(0,0,bitmap.getWidth(),bitmap.getHeight());`这适用于所有设备. (13认同)
  • 如何将clicklistener添加到textview中的不同项目以删除文本. (5认同)
  • 什么是"to_input"? (4认同)

Kri*_*tha 21

这是一个完整的解决方案

//creating textview dynamicalyy
TextView textView=new TextView(context);
textview.setText("Lauren amos");
textview.setbackgroundResource(r.color.urovalshape);
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_cross, 0);


BitmapDrawable dd = (BitmapDrawable) SmsUtil.getDrawableFromTExtView(textView);
edittext.settext(addSmily(dd));

//convert image to spannableString
public SpannableStringBuilder addSmily(Drawable dd) {
 dd.setBounds(0, 0, dd.getIntrinsicWidth(),dd.getIntrinsicHeight());
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(":-)");
builder.setSpan(new ImageSpan(dd), builder.length() - ":-)".length(),builder.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

return builder;
}

  //convert view to drawable
  public static Object getDrawableFromTExtView(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.getWidth(), view.getHeight(),
            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);

}
Run Code Online (Sandbox Code Playgroud)

这是完整的项目文件,如果您有任何想要使用 Spannble的话


Plu*_*rge 5

我有一个库,可以满足您的需求:

  • 默认或完全可自定义(您甚至可以使用自己的布局)
  • 多线支持
  • 单击侦听器

看看这里

这是一个快速入门:

将ChipView添加到您的布局或以编程方式创建它:

<com.plumillonforge.android.chipview.ChipView
    android:id="@+id/chipview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

使用扩展抽象芯片和点击监听器(如果需要)的数据列表初始化它:

List<Chip> chipList = new ArrayList<>();
chipList.add(new Tag("Lorem"));
chipList.add(new Tag("Ipsum dolor"));
chipList.add(new Tag("Sit amet"));
chipList.add(new Tag("Consectetur"));
chipList.add(new Tag("adipiscing elit"));
ChipView chipDefault = (ChipView) findViewById(R.id.chipview);
chipDefault.setChipList(chipList);
chipDefault.setOnChipClickListener(new OnChipClickListener() {
        @Override
        public void onChipClick(Chip chip) {
            // Action here !
        }
    });
Run Code Online (Sandbox Code Playgroud)

默认ChipView的呈现方式如下:

默认的ChipView

但是您可以根据需要从整体到芯片级别进行自定义:

整体ChipView 自定义ChipView

这不是MultiAutocomplete,但你可以设法模仿它(我实际上是这样使用它)