我正在开发一个应用程序,其中选择的联系人被添加到EditText
一个关闭标记图像,当我点击那个关闭标记图像时,联系人应该被删除。我已经完成了显示关闭标记图像的代码,但我不知道如何处理那些关闭标记图像。请建议我如何。
我的代码:
for (int i = 0; i < selectedItems.size(); i++) {
String na = selectedItems.get(i);
TextView tv = createContactTextView(na);
BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(),
bd.getIntrinsicHeight());
sb.append(na + ",");
sb.setSpan(new ImageSpan(bd), sb.length()
- (na.length() + 1), sb.length() - 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
txt.setText(sb);
private Object convertViewToDrawable(TextView 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);
}
private TextView createContactTextView(String text) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setTextSize(25);
tv.setBackgroundResource(R.drawable.oval_small);
tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.close, 0);
return tv;
}