SpannableString与ImageSpan实现文字与图片混合

public class Androidtest2Activity extends Activity {
	
	private EditText mEdit;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
mEdit
= (EditText) this.findViewById(R.id.editText1);
mEdit
.setText( "HelloWorld");Drawable drawable = getResources().getDrawable(R.drawable.icon);drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());SpannableString spannable = new SpannableString(getText().toString() + "[smile]");ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);spannable.setSpan(span, getText().length(),getText().length() + "[smile]".length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);setText(spannable);} private Editable getText() { return
mEdit
.getText();} private void setText(SpannableString spannalbe) {
mEdit
.setText(spannalbe);}}

 
  

你可能感兴趣的:(软件工程)