您的位置:首页 > 移动开发 > Android开发

android 在EditText中显示表情图片

2013-08-14 11:57 330 查看
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐