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;
} }
上一篇:运维ps语法---》ps、pstree、top、htop、nice、renice、kill、ulimit、w 和 who 和 whoami、pgrep、fg 和 bg、ipcs


下一篇:BZOJ - 1003 DP+最短路