android 在EditText中显示表情图片

public class MainActivity extends Activity 
{
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        EditText editText=(EditText) this.findViewById(R.id.edittext);
        
        //src中的图片的名字必须在drawable中存在该图片
        String html="<img src='sleep'>";
        
        CharSequence text=Html.fromHtml(html, new ImageGetter() 
        {
            public Drawable getDrawable(String source)
            {
                Drawable drawable=getResources().getDrawable(getImageID(source));
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth()/3, drawable.getIntrinsicHeight()/3);
                return drawable;
            }
        }, null);
editText.setText(text); }
/* * * 获取图片资源的ID */ public int getImageID(String name) { try { Field field=R.drawable.class.getField(name); return Integer.valueOf(field.getInt(name)); } catch (Exception e) { e.printStackTrace(); } return 0; } }
原文地址:https://www.cnblogs.com/tianshidechibang234/p/3256874.html