本文实例讲述了Android实现EditText中添加和删除bitmap的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
SpannableString mSpan1 = new SpannableString( "1" );
/*
* this is add bitmap on edit text
*/
private void displayBitmapOnText(Bitmap thumbnailBitmap) {
if(thumbnailBitmap == null)
return;
int start = mEditText.getSelectionStart();
mSpan1.setSpan(new ImageSpan(thumbnailBitmap) , mSpan1.length() - 1, mSpan1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// mSpan1.toString();
if(mEditText != null) {
Editable et = mEditText.getText();
et.insert(start, mSpan1);
mEditText.setText(et);
mEditText.setSelection(start + mSpan1.length());
}
mEditText.setLineSpacing(10f, 1f);
}
/*
* this is delete bitmap on edit text
* from end to start
*/
private void deleteEditTextSpan() {
Spanned s = mEditText.getEditableText();
ImageSpan[] imageSpan = s.getSpans( 0 , s.length(), ImageSpan. class );
for ( int i = imageSpan.length - 1 ; i >= 0 ; i--) {
if (i == imageSpan.length - 1 ) {
int start = s.getSpanStart(imageSpan[i]);
int end = s.getSpanEnd(imageSpan[i]);
Editable et = mEditText.getText();
et.delete(start, end);
}
}
mEditText.invalidate();
}
|
ps:关于删除,网上找了很多都是直接调用软键盘,然后再来删除。这种不是我想要的,这个deleteEditTextSpan() 由一个自定义的按键触发。
希望本文所述对大家Android程序设计有所帮助。