我有一个MultiAutoCompleteTextView。我想在onClick方法上达到bubble bubble drawable。
这是我的MultiAutoCompleteTextView类:
TextViewClass.java
public class TextViewClass extends MultiAutoCompleteTextView implements OnItemClickListener, TextWatcher{
Context ActivityContext;
LayoutInflater Inflater;
ArrayList<ItemContact> Contacts;
public TextViewClass(Context ActivityContext) {
super(ActivityContext);
init(ActivityContext);
}
public TextViewClass(Context ActivityContext, AttributeSet Attrs) {
super(ActivityContext, Attrs);
init(ActivityContext);
}
public TextViewClass(Context ActivityContext, AttributeSet Attrs, int DefStyle) {
super(ActivityContext, Attrs, DefStyle);
init(ActivityContext);
}
private void init(Context ActivityContext){
this.ActivityContext = ActivityContext;
this.Contacts = new ArrayList<ItemContact>();
this.setThreshold(0);
this.setTokenizer(new CommaTokenizer());
this.addTextChangedListener(this);
this.setOnItemClickListener(this);
Inflater = (LayoutInflater) ActivityContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
@SuppressWarnings("deprecation")
public void addBubbles(String ContactName){
SpannableStringBuilder SpannableBuilder = new SpannableStringBuilder(getText().toString().replace(",", " "));
LayoutInflater Inflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
TextView tvContacts = (TextView) Inflater.inflate(R.layout.chips_edittext, null);
tvContacts.setText(ContactName);
tvContacts.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.delete_contact, 0);
// capture bitmapt of genreated textview
int Spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
tvContacts.measure(Spec, Spec);
tvContacts.layout(0, 0, tvContacts.getMeasuredWidth(), tvContacts.getMeasuredHeight());
Bitmap ContactBitmap = Bitmap.createBitmap(tvContacts.getWidth(), tvContacts.getHeight(), Bitmap.Config.ARGB_8888);
Canvas ContactCanvas = new Canvas(ContactBitmap);
ContactCanvas.translate(-tvContacts.getScrollX(), -tvContacts.getScrollY());
tvContacts.draw(ContactCanvas);
tvContacts.setDrawingCacheEnabled(true);
Bitmap CacheBitmap = tvContacts.getDrawingCache();
Bitmap ViewBitmap = CacheBitmap.copy(Bitmap.Config.ARGB_8888, true);
tvContacts.destroyDrawingCache();
BitmapDrawable BitmapDrawable = new BitmapDrawable(ViewBitmap);
BitmapDrawable.setBounds(0, 0, BitmapDrawable.getIntrinsicWidth(), BitmapDrawable.getIntrinsicHeight());
ImageSpan Span = new ImageSpan(BitmapDrawable);
SpannableBuilder.setSpan(Span, SpannableBuilder.length() - ContactName.length() - 1, SpannableBuilder.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(SpannableBuilder);
setSelection(getText().length());
}
@SuppressWarnings("deprecation")
public void setBubbles(){
SpannableStringBuilder SpannableBuilder = new SpannableStringBuilder(getText().toString());
String Contacts[] = getText().toString().trim().split(",");
int x = 0;
if(Contacts.length > 0){
for(int i = 0; i < Contacts.length; i ++){
if(!Contacts[i].equals("")){
final String Contact = Contacts[i].toString();
LayoutInflater Inflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
TextView tvContacts = (TextView) Inflater.inflate(R.layout.chips_edittext, null);
tvContacts.setText(Contact);
tvContacts.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.delete_contact, 0);
// capture bitmapt of genreated textview
int Spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
tvContacts.measure(Spec, Spec);
tvContacts.layout(0, 0, tvContacts.getMeasuredWidth(), tvContacts.getMeasuredHeight());
Bitmap ContactBitmap = Bitmap.createBitmap(tvContacts.getWidth(), tvContacts.getHeight(), Bitmap.Config.ARGB_8888);
Canvas ContactCanvas = new Canvas(ContactBitmap);
ContactCanvas.translate(-tvContacts.getScrollX(), -tvContacts.getScrollY());
tvContacts.draw(ContactCanvas);
tvContacts.setDrawingCacheEnabled(true);
Bitmap CacheBitmap = tvContacts.getDrawingCache();
Bitmap ViewBitmap = CacheBitmap.copy(Bitmap.Config.ARGB_8888, true);
tvContacts.destroyDrawingCache();
BitmapDrawable BitmapDrawable = new BitmapDrawable(ViewBitmap);
BitmapDrawable.setBounds(0, 0, BitmapDrawable.getIntrinsicWidth(), BitmapDrawable.getIntrinsicHeight());
ImageSpan Span = new ImageSpan(BitmapDrawable);
SpannableBuilder.setSpan(Span, x, x + Contact.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
x = x + Contact.length() + 1;
} else {
x = x + 1;
}
}
setText(SpannableBuilder);
setSelection(getText().length());
}
}
@Override
public void beforeTextChanged(CharSequence CSequence, int Start, int Count, int After) {
}
@Override
public void onTextChanged(CharSequence CSequence, int Start, int Before, int Count) {
if(Count > 0)
if(CSequence.charAt(Start) == ',')
setBubbles();
}
@Override
public void afterTextChanged(Editable Edit) {
}
我想通过点击ımage删除气泡;
我已经研究过,找到了 ClickableSpan ,但我没有设置此代码..请帮帮我..