final EditText editface=(EditText)
this.findViewById(R.id.EditText);
List<Map<String,Object>> list=
getGridData();
GridView grid=(GridView) View.inflate(
this,R.layout.pop,
null).findViewById(R.id.gridview);;
grid.setAdapter(new MyAdapter(
this, list));
final Dialog dialog=
new Dialog(
this, R.style.FullScreenDialog);
dialog.setContentView(grid);
dialog.show();
grid.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1,
int position,
long arg3)
{
String html=
"<img src="+list.
get(position).
get(
"name")+
" />";
CharSequence Text=Html.fromHtml(html,
new ImageGetter()
{
public Drawable getDrawable(String source)
{
Drawable drawable=
getResources().getDrawable(getResourceID(source));
drawable.setBounds(0,
0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
return drawable;
}
}, null);
dialog.dismiss();
editface.append(Text);
}
});
//根据资源的名字找到他的Id号
public int getResourceID(String name)
{
System.out.println(name);
try
{
Field field=R.drawable.
class.getField(name);
return Integer.valueOf(field.getInt(name));
} catch (Exception e)
{
e.printStackTrace();
}
return 0;
}
public List<Map<String,Object>>
getGridData()
{
List<Map<String,Object>> list=
new ArrayList<Map<String,Object>>
();
try
{
Field[] field=R.drawable.
class.getFields();
for(Field f:field)
{
if(f.getName().indexOf(
"f0")!=-
1)
{
Map<String, Object> map=
new HashMap<String,Object>
();
int id=f.getInt(R.drawable.
class);
map.put("image", id);
//src中的图片的名字必须在drawable中存在该图片
map.put(
"name", f.getName().toString());
list.add(map);
}
}
}catch (Exception e) {
e.printStackTrace();
}
return list;
}
效果图:
转载于:https://www.cnblogs.com/tianshidechibang234/p/3260351.html