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 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);
}
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);
tv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Drawable co = ((TextView) v).getCompoundDrawables()[2];
if (event.getX() > v.getMeasuredWidth() - v.getPaddingRight()
- co.getIntrinsicWidth()) {
//tv.setText("");
Log.i(TAG, "clicked on delete button");
return true;
} else {
return false;
}
}
});
return tv;
}
editText 내에 텍스트가있는 범위를 만들지 만 해당 범위에서 삭제 버튼을 클릭 할 때 삭제하려고합니다. 나는 그것을 "tv.setOnTouchListener (새 OnTouchListener() {................"에 대한 코드를 작성 createContactTextView 기능에 있지만 작동하지 않습니다. }editText 범위에서 해당 삭제 버튼을 클릭하면 요소가 삭제됩니다.
을 ... 공유하세요 코드는 솔루션을 찾은 경우 ... – VijayRaj
물론 않습니다 아닙니다. 텍스트보기는 직접 사용되지 않습니다. TextView에는 OnTouchListener가 있지만 텍스트보기의 비트 맵 만 표시된 후에는 더 이상 EventListner가 없습니다. – Servus7