work_weipa_EditText使用表情功能

问题:如何实现点击一张图片,从底部弹出表情栏,选择表情,在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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public final void M() { List split$default; com.niming.weipa.c.a.a(this.activity, getUserInfo2().getAvatar(), (ImageView) _$_findCachedViewById(R.id.iv_user_avatar)); TextView tv_user_name = (TextView) _$_findCachedViewById(R.id.tv_user_name); Intrinsics.checkExpressionValueIsNotNull(tv_user_name, "tv_user_name"); tv_user_name.setText(getUserInfo2().getNick()); if (getUserInfo2().getRank_type() == 0) { ImageView ivVipLevel = (ImageView) _$_findCachedViewById(R.id.ivVipLevel); Intrinsics.checkExpressionValueIsNotNull(ivVipLevel, "ivVipLevel"); ivVipLevel.setVisibility(8); } else { ((ImageView) _$_findCachedViewById(R.id.ivVipLevel)).setImageResource(com.niming.weipa.utils.j.a(getUserInfo2().getRank_type())); } if (Intrinsics.areEqual(getUserInfo2().getIs_vip(), "y")) { String vip_expired = getUserInfo2().getVip_expired(); Intrinsics.checkExpressionValueIsNotNull(vip_expired, "userInfo2.vip_expired"); split$default = StringsKt__StringsKt.split$default((CharSequence) vip_expired, new String[]{ConstantUtils.PLACEHOLDER_STR_ONE}, false, 0, 6, (Object) null); TextView tv_user_sub_title = (TextView) _$_findCachedViewById(R.id.tv_user_sub_title); Intrinsics.checkExpressionValueIsNotNull(tv_user_sub_title, "tv_user_sub_title"); tv_user_sub_title.setText("已开通" + getUserInfo2().getRank_type_str() + ' ' + ((String) split$default.get(0)) + "到期"); return; } TextView tv_user_sub_title2 = (TextView) _$_findCachedViewById(R.id.tv_user_sub_title); Intrinsics.checkExpressionValueIsNotNull(tv_user_sub_title2, "tv_user_sub_title"); tv_user_sub_title2.setText("您当前未开通抖阴VIP"); 上面这段代码干了什么,哪里是关键判断点。
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值