<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <! -- Chat input --> <edittext Android: Id = "@ + ID/et_chat" Android: layout_width = "300dip" Android: layout_height = "wrap_content"/> <button Android: id = "@ + ID/bt_insetface" Android: layout_width = "50dip" Android: layout_height = "wrap_content" Android: text = "insert tag"/> </linearlayout>
EditTextFaceActivity .java
Package CN. face; import Java. lang. reflect. field; // reflect reflection import Java. util. random; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. drawable. bitmapdrawable; import android. OS. bundle; import android. text. spannable; import android. text. spannablestring; import android. text. style. imagespan; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; public class edittextfaceactivity extends activity {private edittext et_chat; private button bt_insetface; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); et_chat = (edittext) findviewbyid (R. id. et_chat); bt_insetface = (button) findviewbyid (R. id. bt_insetface); bt_insetface.setonclicklistener (New onclicklistener () {public void onclick (view v) {int randomid = 1 + new random (). nextint (20); try {field = R. drawable. class. getdeclaredfield ("face" + randomid); int resourceid = integer. parseint (field. get (null ). tostring (); // display the image information in Android. Bitmap bitmap must be used to load bitmap = bitmapfactory. decoderesource (getresources (), resourceid); imagespan = new imagespan (edittextfaceactivity. this, bitmap); spannablestring = new spannablestring ("face"); // the prefix of the emoticon image is "face" spannablestring. setspan (imagespan, 0, 4, spannable. span_exclusive_exclusive); et_chat.append (spannablestring);} catch (exception e ){}}});}}
View implementation Images