Reference: http://blog.csdn.net/wulianghuan/article/details/8583921
Entering an emoticon in the input box is a required function of each chat software. To do this, you only need to put the emoticon in the project image folder and then use this code to add images, that is to say, add the image as a text to the input box.
// Obtain the emoticon Image File Name field = R. drawable. class. getdeclaredfield ("F" + randomid); int resourceid = integer. parseint (field. get (null ). tostring (); // to display image information in Android, you must use the bitmap object to load Bitmap bitmap = bitmapfactory. decoderesource (getresources (), resourceid); // you must use imagespan = new imagespan (mainactivity. this, bitmap); // F indicates the image prefix name spannablestring = new spannablestring ("F"); // you can specify the font spannablestring. setspan (imagespan, 0, 1, spannable. span_exclusive_exclusive); edittext. append (spannablestring );
Layout File
<? 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"> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Id = "@ + ID/edittext" Android: layout_margintop = "10dp"/> <button Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Id = "@ + ID/button" Android: text = "add emoticon"/> <textview Android: id = "@ + ID/textview1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "use Android: digits attribute (enter a number) "/> <edittext Android: layout_width =" 200dp "Android: layout_height =" wrap_content "Android: Id =" @ + ID/edittext1 "Android: layout_margin =" 10dp "Android: digits = "0123456789" Android: layout_margintop = "10dp"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: id = "@ + ID/textview2" Android: text = "Use the Android: digits attribute (Enter 26 lowercase letters)"/> <edittext Android: layout_width = "200dp" Android: layout_height = "wrap_content" Android: Id = "@ + ID/edittext2" Android: layout_margin = "10dp" Android: digits = "abcdefghijklmnopqrstuvwxyz" Android: layout_margintop = "10dp"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Id = "@ + ID/textview3" Android: TEXT = "use Android: inputtype attribute (input number)"/> <edittext Android: layout_width = "200dp" Android: layout_height = "wrap_content" Android: id = "@ + ID/edittext3" Android: layout_margin = "10dp" Android: inputtype = "number | textcapcharacters" Android: layout_margintop = "10dp"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Id = "@ + ID/textview4" Android: text = "Use the Android: inputtype attribute (input email) "/> <edittext Android: layout_width =" 200dp "Android: layout_height =" wrap_content "Android: Id =" @ + ID/edittext4 "Android: layout_margin =" 10dp "Android: inputtype = "textemailaddress" Android: layout_margintop = "10dp"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: id = "@ + ID/textview5" Android: text = "Use the Android: inputtype attribute (enter a signed floating point number)"/> <edittext Android: layout_width = "200dp" Android: layout_height = "wrap_content" Android: Id = "@ + ID/edittext5" Android: layout_margin = "10dp" Android: Numeric = "decimal | signed" Android: layout_margintop = "10dp"/> </linearlayout>
Mainactivity. Java
Package COM. kale. edittext02; import Java. lang. reflect. field; import Java. util. random; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. OS. bundle; import android. support. v7.app. actionbaractivity; import android. text. spannable; import android. text. spannablestring; import android. text. style. imagespan; import android. view. menu; import android. view. menuitem; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; public class mainactivity extends actionbaractivity {private edittext; private button; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); edittext = (edittext) findviewbyid (R. id. edittext); button = (button) findviewbyid (R. id. button); button. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {// the random number generated is from 0 int randomid = new random (). nextint (7); // The value range of nextint (7) is 0-6 try {// obtain the emoticon Image File Name field = R. drawable. class. getdeclaredfield ("F" + randomid); int resourceid = integer. parseint (field. get (null ). tostring (); // to display image information in Android, you must use the bitmap object to load Bitmap bitmap = bitmapfactory. decoderesource (getresources (), resourceid); // you must use imagespan = new imagespan (mainactivity. this, bitmap); // F indicates the image prefix name spannablestring = new spannablestring ("F"); // you can specify the font spannablestring. setspan (imagespan, 0, 1, spannable. span_exclusive_exclusive); edittext. append (spannablestring);} catch (securityexception e) {e. printstacktrace ();} catch (nosuchfieldexception e) {e. printstacktrace ();} catch (exception e) {e. printstacktrace ();}}});}}
Source code download: http://download.csdn.net/detail/shark0017/7623047