问题:如何实现点击一张图片,从底部弹出表情栏,选择表情,在EditText中显示?
回答:先写一个表情的工具类,然后调用即可
例子:
工具类:
public class ExpressionUtils {
public static void dealExpression(Context context,SpannableString spannableString, Pattern patten, int start) throws SecurityException, NoSuchFieldException, NumberFormatException, IllegalArgumentException, IllegalAccessException {
Matcher matcher = patten.matcher(spannableString);
while (matcher.find()) {
String key = matcher.group();
if (matcher.start() < start) {
continue;
}
Field field = R.drawable.class.getDeclaredField(key);
int resId = Integer.parseInt(field.get(null).toString()); //通过上面匹配得到的字符串来生成图片资源id
if (resId != 0) {
Drawable d = context.getResources().getDrawable(resId);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); //通过图片资源id来得到bitmap,用一个ImageSpan来包装
int end = matcher.start() + key.length(); //计算该图片名字的长度,也就是要替换的字符串的长度
spannableString.setSpan(span, matcher.start(), end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将该图片替换字符串中规定的位置中
if (end < spannableString.length()) { //如果整个字符串还未验证完,则继续。。
dealExpression(context,spannableString, patten, end);
}
break;
}
}
}
/**
* 得到一个SpanableString对象,通过传入的字符串,并进行正则判断
*/
public static SpannableString getExpressionString(Context context,String str,String zhengze){
SpannableString spannableString = new SpannableString(str);
Pattern sinaPatten = Pattern.compile(zhengze, Pattern.CASE_INSENSITIVE); //通过传入的正则表达式来生成一个pattern
try {
dealExpression(context,spannableString, sinaPatten, 0);
} catch (Exception e) {
Log.e("dealExpression", e.getMessage());
}
return spannableString;
}
public class SelectPicPopupWindow extends PopupWindow {
private View mMenuView;
private int[] imageIds = new int[107];
private Context context;
private List<Map<String,Object>> listItems;
public List<Map<String, Object>> getListItems() {
return listItems;
}
public View getmMenuView() {
return mMenuView;
}
public SelectPicPopupWindow(Context context){
super(context);
this.context = context;
}
public GridView createGridView(){
final GridView cgv_face = new GridView(context);
selectPicPopupWindow(context);
SimpleAdapter simpleAdapter = new SimpleAdapter(context, listItems, R.layout.team_layout_single_expression_cell, new String[]{"image"}, new int[]{R.id.image});
cgv_face.setAdapter(simpleAdapter);
cgv_face.setBackgroundColor(Color.WHITE);
cgv_face.setNumColumns(6);
cgv_face.setHorizontalSpacing(0);
cgv_face.setVerticalSpacing(0);
cgv_face.setVerticalScrollBarEnabled(true);
return cgv_face;
}
public void selectPicPopupWindow(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.alert_dialog, null);
this.setContentView(mMenuView);
this.setWidth(720);
this.setHeight(500);
this.setFocusable(true);
this.setAnimationStyle(R.style.AnimBottom);
ColorDrawable dw = new ColorDrawable(0xffffff);
this.setBackgroundDrawable(dw);
listItems = new ArrayList<Map<String,Object>>();
//生成107个表情的id,封装
for(int i = 0; i < 107; i++){
try {
if(i<10){
Field field = R.drawable.class.getDeclaredField("f00" + i);
int resourceId = Integer.parseInt(field.get(null).toString());
imageIds[i] = resourceId;
}else if(i<100){
Field field = R.drawable.class.getDeclaredField("f0" + i);
int resourceId = Integer.parseInt(field.get(null).toString());
imageIds[i] = resourceId;
}else{
Field field = R.drawable.class.getDeclaredField("f" + i);
int resourceId = Integer.parseInt(field.get(null).toString());
imageIds[i] = resourceId;
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Map<String,Object> listItem = new HashMap<String,Object>();
listItem.put("image", imageIds[i]);
listItems.add(listItem);
}
}
实现代码
menuWindow = new SelectPicPopupWindow(getActivity());
et_repeat = (EditText) v.findViewById(R.id.et_repeat);
gv_face = menuWindow.createGridView();
gv_face.setVerticalScrollBarEnabled(true);
iv_ex = (ImageView) v.findViewById(R.id.iv_ex);
iv_ex.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
menuWindow.setContentView(gv_face);
menuWindow.showAtLocation(v, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
gv_face.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String str = null;
if(arg2<10){
str = "f00"+arg2;
}else if(arg2<100){
str = "f0"+arg2;
}else{
str = "f"+arg2;
}
String zhengze = "f0[0-9]{2}|f10[0-7]";
SpannableString expressionString = ExpressionUtils.getExpressionString(getActivity(), str, zhengze);
et_repeat.append(expressionString);
menuWindow.dismiss();
}
});
}
});
参考网址:http://blog.csdn.net/duancanmeng/article/details/7677144