android 在EditText中显示表情图片

时间:2023-03-09 15:13:02
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;
} }