EditView中插入图片

package com.imo;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Html.ImageGetter;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Android_demoActivity extends Activity {
	private TextView text;
	private EditText edit;
	private ImageGetter imageGetter = new ImageGetter() {
		public Drawable getDrawable(String source) {
			int id = Integer.parseInt(source);
			Drawable d = getResources().getDrawable(id);
			d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			return d;
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		edit = (EditText) findViewById(R.id.edit);
		text = (TextView) findViewById(R.id.text);
		insertPic2();
		text.setText(edit.getText());
		insertPic1();
	}

	public void click(View view) {
		System.out.println(edit.getText());
	}

	/**
	 * 第一种插入图片方式
	 */
	private void insertPic1() {
		SpannableString ss = new SpannableString("pic");
		Drawable d = getResources().getDrawable(R.drawable.icon);
		d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
		ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
		ss.setSpan(span, 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		edit.append(ss);
	}

	/**
	 * 第二种插入图片方式
	 */
	private void insertPic2() {
		edit.append(Html.fromHtml("", imageGetter, null));
	}

}





	
	

	


你可能感兴趣的:(Android,UI)